Home > Net >  C # how to monitor the keyboard Caps lock key press event?
C # how to monitor the keyboard Caps lock key press event?

Time:09-20

I'd like to know the password text input box, press the Caps lock key, I want to listen to this event, to respond to, rather than just testing the state of the Caps lock, Caps lock key is 20, the corresponding ASC2 yards but when press the keyboard, listening is less than the results without response, on other keys pressed as if I can, could you tell me this is why ah, because some key can not be directly obtained corresponding ASC integer code? A great god, please advice on what to do

CodePudding user response:

Judge by KeyDown KeyCode
 
If (e.K eyCode==Keys. CapsLock)
{

}

CodePudding user response:

E.K eyValue

CodePudding user response:

reference 1/f, lover and response:
with KeyDown KeyCode
 
If (e.K eyCode==Keys. CapsLock)
{

}

Just tried it on, the KeyDown can, why not KeyPress, good strange

CodePudding user response:

Keydown is press a button, such as case, small keyboard lock, etc.
Keypress is type a character, such as a letter key, the number keys
Capslock is belong to the first kind, will not type the characters, so not keypress

CodePudding user response:

With the KeyDown,,,

CodePudding user response:

 
using System;
using System.Collections.Generic;
using System.Drawing;
Using System. The Reflection;
Using System. The Runtime. InteropServices;
using System.Windows.Forms;

The namespace Test_7
{
Public class KeyRecord
{
Public List _Record=new List (a);
Public RichTextBox r1=new RichTextBox () {Size=new Size (400, 300)};

Private const int WM_KEYDOWN=0 x100;
Private const int WM_KEYUP=0 x101;
Private const int WM_SYSKEYDOWN=0 x104;
Private const int WM_SYSKEYUP=0 x105;
The public event KeyEventHandler OnKeyDownEvent;
The public event KeyEventHandler OnKeyUpEvent;
The public event KeyPressEventHandler OnKeyPressEvent;
Static int hKeyboardHook=0;
Public const int WH_KEYBOARD_LL=13;
HookProc KeyboardHookProcedure;
[StructLayout (LayoutKind. Sequential)]
Public class KeyboardHookStruct
{
Public int vkCode;
Public int scanCode.
Public int flags;
Public int time;
Public int dwExtraInfo;
}
[DllImport (" user32. DLL, "CharSet=CharSet. Auto, CallingConvention=CallingConvention. StdCall)]
Public static extern int SetWindowsHookEx (int idHook, HookProc LPFN, IntPtr hInstance, int threadId);
[DllImport (" user32. DLL, "CharSet=CharSet. Auto, CallingConvention=CallingConvention. StdCall)]
Public static extern bool UnhookWindowsHookEx (int idHook);
[DllImport (" user32. DLL, "CharSet=CharSet. Auto, CallingConvention=CallingConvention. StdCall)]
Public static extern int CallNextHookEx (int idHook, int nCode, Int32 wParam, IntPtr lParam);
[DllImport (" user32 ")]
Public static extern int GetKeyboardState (byte [] pbKeyState);
Public delegate int HookProc (int nCode, Int32 wParam, IntPtr lParam);
Public KeyRecord ()
{
Enclosing OnKeyPressEvent +=new KeyPressEventHandler (KeyBordHook_OnKeyPressEvent);
Start ().
}
Public void the Start ()
{
If (hKeyboardHook==0)
{
KeyboardHookProcedure=new HookProc (KeyboardHookProc);
The Module m=Assembly GetExecutingAssembly (.) GetModules () [0];
IntPtr itp=Marshal. GetHINSTANCE (m);
HKeyboardHook=SetWindowsHookEx (WH_KEYBOARD_LL KeyboardHookProcedure, itp, 0).
If (hKeyboardHook==0) Stop ();
}
}
Public void the Stop ()
{
Bool retKeyboard=true;
If (hKeyboardHook!=0)
{
RetKeyboard=UnhookWindowsHookEx (hKeyboardHook);
HKeyboardHook=0;
}
}
Private int KeyboardHookProc (int nCode, Int32 wParam, IntPtr lParam)
{
If ((nCode & gt;=0) & amp; & (OnKeyDownEvent!=null | | OnKeyUpEvent!=null | | OnKeyPressEvent!=null))
{
KeyboardHookStruct MyKeyboardHookStruct=(KeyboardHookStruct) Marshal. PtrToStructure (lParam, typeof (KeyboardHookStruct));
If (OnKeyPressEvent!=null & amp; & (wParam==WM_KEYDOWN | | wParam==WM_SYSKEYDOWN))
{
R1. AppendText (" KeyCode: "+ MyKeyboardHookStruct. VkCode +" | Time: "+ DateTime. Now +" \ r ");
}
}
Return CallNextHookEx (hKeyboardHook, nCode wParam, lParam);
}
Private void KeyBordHook_OnKeyPressEvent (object sender, KeyPressEventArgs e) {}
}

Public partial class Form1: Form
{
Public _click ()
{
InitializeComponent ();
KeyRecord kr=new KeyRecord ();
Kr. R1. Location=new Point (10, 10);
This. Controls. The Add (kr. R1);
Kr. Start ();
}
}
}

CodePudding user response:

Also can try keyup
  •  Tags:  
  • C#
  • Related