Delphi TASTERI
Delphi TASTERI
Type TShiftState = set of (ssShift, ssAlt, ssCtrl, ssLeft, ssRight, ssMiddle, ssDouble);
That is to say, the TShiftstate type has ssShift, ssAlt, ssCtrl, ssLeft (left mouse button), ssRight
(right mouse button), ssMiddle (mouse middle button), ssDouble (mouse double click)
Value Meaning
Used to determine whether the combination key uses shift, Ctrl, left mouse button, right button,
middle button, double click event.
The Key in the OnKeyDown and OnKeyUp events is Word type, indicating which key on the
keyboard the user presses. The Key in OnKeyPress is Char, indicating what characters the user
enters. One produces 8 (char) regular health values and one produces 16 (word) functional health
values.
1.KeyPress is mainly used to capture numbers (note: including Shift+number symbols), letters
(note: including capitalization), keypad, etc. except F1-12, SHIFT, Alt, Ctrl, Insert, Home, PgUp,
Delete, End , PgDn, ScrollLock, Pause, NumLock, {menu key}, {start key} and ANSI characters
outside the arrow keys, KeyDown and KeyUp can usually capture all keyboard keys except
PrScrn (special keys for special keyboards are not discussed here)
4. KeyDown and KeyUp The KeyValue captured for a single character is a value, that is, the
case of a single character cannot be determined.
5. KeyPress does not distinguish between numeric characters on the keypad and the main
keyboard.
KeyDown and KeyUp distinguish between the numeric characters of the keypad and the main
keyboard.
Keypress is the keyboard pressing any number key and any letter key
1 onkeydown event
Type TKeyEvent = procedure (Sender: TObject; var Key: Word; Shift: TShiftState) of object;
This event occurs when any key is pressed. Look! It responds to the Word type and recognizes
the Shift key state.
2 onkeypress event
Among them, OnKeyDown and OnKeyUp respond to any button, and the incoming parameter is
Shift: TShiftState represents the state of the control key, including Ctrl, Shift, Alt on the
keyboard and the left, center, right and double click of the mouse.
Onkeypress event
If key=#13 then // trigger this event when you press the enter key
Do something
Onkeydown event
If key=13 then // trigger this event when you press the enter key
Do something
...
Keybd_event(VK_TAB,2,0,0);
Perform(WM_NEXTDLGCTL,0,0);
...
But obviously the return code TAB is obviously not suitable for BUTTON. When you press
Enter on it, it will execute OnClick, so it will not TAB, you can use other keys instead of Enter.
》》》》》》》》》》
Key value
Showmessage(inttostr(key));
Showmessage(key);
I will know.
Form1.KeyPreview := True;
Shift: TShiftState);
Begin
Showmessage(IntToStr(Key));
End;
Procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
Begin
Showmessage(IntToStr(Ord(Key)));
End;
{$EXTERNALSYM VK_LBUTTON}
VK_LBUTTON = 1;
{$EXTERNALSYM VK_RBUTTON}
VK_RBUTTON = 2;
{$EXTERNALSYM VK_CANCEL}
VK_CANCEL = 3;
{$EXTERNALSYM VK_MBUTTON}
{$EXTERNALSYM VK_BACK}
VK_BACK = 8;
{$EXTERNALSYM VK_TAB}
VK_TAB = 9;
{$EXTERNALSYM VK_CLEAR}
VK_CLEAR = 12;
{$EXTERNALSYM VK_RETURN}
VK_RETURN = 13;
{$EXTERNALSYM VK_SHIFT}
VK_SHIFT = $10;
{$EXTERNALSYM VK_CONTROL}
VK_CONTROL = 17;
{$EXTERNALSYM VK_MENU}
VK_MENU = 18;
{$EXTERNALSYM VK_PAUSE}
VK_PAUSE = 19;
{$EXTERNALSYM VK_CAPITAL}
VK_CAPITAL = 20;
{$EXTERNALSYM VK_KANA }
VK_KANA = 21;
{$EXTERNALSYM VK_HANGUL }
VK_HANGUL = 21;
{$EXTERNALSYM VK_JUNJA }
VK_JUNJA = 23;
{$EXTERNALSYM VK_FINAL }
VK_FINAL = 24;
{$EXTERNALSYM VK_HANJA }
VK_HANJA = 25;
{$EXTERNALSYM VK_KANJI }
VK_KANJI = 25;
{$EXTERNALSYM VK_CONVERT }
VK_CONVERT = 28;
{$EXTERNALSYM VK_NONCONVERT }
VK_NONCONVERT = 29;
{$EXTERNALSYM VK_ACCEPT }
VK_ACCEPT = 30;
{$EXTERNALSYM VK_MODECHANGE }
VK_MODECHANGE = 31;
{$EXTERNALSYM VK_ESCAPE}
VK_ESCAPE = 27;
{$EXTERNALSYM VK_SPACE}
VK_SPACE = $20;
{$EXTERNALSYM VK_PRIOR}
VK_PRIOR = 33;
{$EXTERNALSYM VK_NEXT}
VK_NEXT = 34;
{$EXTERNALSYM VK_END}
VK_END = 35;
{$EXTERNALSYM VK_HOME}
VK_HOME = 36;
{$EXTERNALSYM VK_LEFT}
VK_LEFT = 37;
{$EXTERNALSYM VK_UP}
VK_UP = 38;
{$EXTERNALSYM VK_RIGHT}
VK_RIGHT = 39;
{$EXTERNALSYM VK_DOWN}
VK_DOWN = 40;
{$EXTERNALSYM VK_SELECT}
VK_SELECT = 41;
{$EXTERNALSYM VK_PRINT}
VK_PRINT = 42;
{$EXTERNALSYM VK_EXECUTE}
VK_EXECUTE = 43;
{$EXTERNALSYM VK_SNAPSHOT}
VK_SNAPSHOT = 44;
{$EXTERNALSYM VK_INSERT}
VK_INSERT = 45;
{$EXTERNALSYM VK_DELETE}
VK_DELETE = 46;
{$EXTERNALSYM VK_HELP}
VK_HELP = 47;
{ VK_0 thru VK_9 are the same as ASCII ’0’ thru ’9’ ($30 - $39) }
{$EXTERNALSYM VK_LWIN}
VK_LWIN = 91;
{$EXTERNALSYM VK_RWIN}
VK_RWIN = 92;
{$EXTERNALSYM VK_APPS}
VK_APPS = 93;
{$EXTERNALSYM VK_NUMPAD0}
VK_NUMPAD0 = 96;
{$EXTERNALSYM VK_NUMPAD1}
VK_NUMPAD1 = 97;
{$EXTERNALSYM VK_NUMPAD2}
VK_NUMPAD2 = 98;
{$EXTERNALSYM VK_NUMPAD3}
VK_NUMPAD3 = 99;
{$EXTERNALSYM VK_NUMPAD4}
VK_NUMPAD4 = 100;
{$EXTERNALSYM VK_NUMPAD5}
VK_NUMPAD5 = 101;
{$EXTERNALSYM VK_NUMPAD6}
VK_NUMPAD6 = 102;
{$EXTERNALSYM VK_NUMPAD7}
VK_NUMPAD7 = 103;
{$EXTERNALSYM VK_NUMPAD8}
VK_NUMPAD8 = 104;
{$EXTERNALSYM VK_NUMPAD9}
VK_NUMPAD9 = 105;
{$EXTERNALSYM VK_MULTIPLY}
VK_MULTIPLY = 106;
{$EXTERNALSYM VK_ADD}
VK_ADD = 107;
{$EXTERNALSYM VK_SEPARATOR}
VK_SEPARATOR = 108;
{$EXTERNALSYM VK_SUBTRACT}
VK_SUBTRACT = 109;
{$EXTERNALSYM VK_DECIMAL}
VK_DECIMAL = 110;
{$EXTERNALSYM VK_DIVIDE}
VK_DIVIDE = 111;
{$EXTERNALSYM VK_F1}
VK_F1 = 112;
{$EXTERNALSYM VK_F2}
VK_F2 = 113;
{$EXTERNALSYM VK_F3}
VK_F3 = 114;
{$EXTERNALSYM VK_F4}
VK_F4 = 115;
{$EXTERNALSYM VK_F5}
VK_F5 = 116;
{$EXTERNALSYM VK_F6}
VK_F6 = 117;
{$EXTERNALSYM VK_F7}
VK_F7 = 118;
{$EXTERNALSYM VK_F8}
VK_F8 = 119;
{$EXTERNALSYM VK_F9}
VK_F9 = 120;
{$EXTERNALSYM VK_F10}
VK_F10 = 121;
{$EXTERNALSYM VK_F11}
VK_F11 = 122;
{$EXTERNALSYM VK_F12}
VK_F12 = 123;
{$EXTERNALSYM VK_F13}
VK_F13 = 124;
{$EXTERNALSYM VK_F14}
VK_F14 = 125;
{$EXTERNALSYM VK_F15}
VK_F15 = 126;
{$EXTERNALSYM VK_F16}
VK_F16 = 127;
{$EXTERNALSYM VK_F17}
VK_F17 = 128;
{$EXTERNALSYM VK_F18}
VK_F18 = 129;
{$EXTERNALSYM VK_F19}
VK_F19 = 130;
{$EXTERNALSYM VK_F20}
VK_F20 = 131;
{$EXTERNALSYM VK_F21}
VK_F21 = 132;
{$EXTERNALSYM VK_F22}
VK_F22 = 133;
{$EXTERNALSYM VK_F23}
VK_F23 = 134;
{$EXTERNALSYM VK_F24}
VK_F24 = 135;
{$EXTERNALSYM VK_NUMLOCK}
VK_NUMLOCK = 144;
{$EXTERNALSYM VK_SCROLL}
VK_SCROLL = 145;
{ VK_L & VK_R - left and right Alt, Ctrl and Shift virtual keys.
No other API or message will distinguish left and right keys in this way. }
{$EXTERNALSYM VK_LSHIFT}
VK_LSHIFT = 160;
{$EXTERNALSYM VK_RSHIFT}
VK_RSHIFT = 161;
{$EXTERNALSYM VK_LCONTROL}
VK_LCONTROL = 162;
{$EXTERNALSYM VK_RCONTROL}
VK_RCONTROL = 163;
{$EXTERNALSYM VK_LMENU}
VK_LMENU = 164;
{$EXTERNALSYM VK_RMENU}
VK_RMENU = 165;
{$EXTERNALSYM VK_PROCESSKEY}
VK_PROCESSKEY = 229;
{$EXTERNALSYM VK_ATTN}
VK_ATTN = 246;
{$EXTERNALSYM VK_CRSEL}
VK_CRSEL = 247;
{$EXTERNALSYM VK_EXSEL}
VK_EXSEL = 248;
{$EXTERNALSYM VK_EREOF}
VK_EREOF = 249;
{$EXTERNALSYM VK_PLAY}
VK_PLAY = 250;
{$EXTERNALSYM VK_ZOOM}
VK_ZOOM = 251;
{$EXTERNALSYM VK_NONAME}
VK_NONAME = 252;
{$EXTERNALSYM VK_PA1}
VK_PA1 = 253;
{$EXTERNALSYM VK_OEM_CLEAR}
VK_OEM_CLEAR = 254;
:), in fact, do not remember, you are not using delphi, enter vk_f1 always in the ide, then hold
down the ctrl key
As for the value of key:char, it is the ascII code of key:word. You can see chr(vk_f1),
chr(vk_enter), all can!
》》》》》》》》》》》》》》》》》》
Vk_f1 f1 key
Vk_f2 f2 key
Vk_f3 f3 key
Vk_f4 f4 key
Vk_f5 f5 key
Vk_f6 f6 key
Vk_f7 f7 key
Vk_f8 f8 key
Vk_f9 f9 key
Vk_lshift left shift key (only used with getasynckeystate and getkeystate)
Vk_rshift right shift key (only used with getasynckeystate and getkeystate)
Vk_lcontrol left ctrl key (only used with getasynckeystate and getkeystate)
Vk_rcontrol right ctrl key (only used with getasynckeystate and getkeystate)
Vk_lmenu left alt key (only used with getasynckeystate and getkeystate)
Vk_rmenu right alt key (only used with getasynckeystate and getkeystate)
Analog mouse
Mouse_event(MOUSEEVENTF_MOVE,dx,dy,0, 0 );
Setcursorpos(x,y);
Mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0 );
Mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0,
0, 0 );
Mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 );
Mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0 );
Mouse double click is simulated by two presses and release
Keyboard simulation
The general virtual key value (VK_) is obtained by the KeyDown or keyup event.
Tab--9
Shift--16
Ctrl--17
Alt--18
CapsLock--20
Esc--27
Win--91,92
NumLock--144
ScrollLock--145
Release button
Keybd_event(key, MapVirtualKey(key, 0 ), 0 , 0 );
The above method works for most keys, and a few keys use different values.
》》》》》》》》》》》》》》》》》》》》》》》》》
VK_TAB 09 9 Tab
VK_SPACE 20 32 Spacebar
VK_PRINT 2A 42
VK_0 30 48 0 button
VK_1 31 49 1 button
VK_2 32 50 2 button
VK_3 33 51 3 button
VK_4 34 52 4 button
VK_5 35 53 5 button
VK_6 36 54 6 button
VK_7 37 55 7 button
VK_8 38 56 8 button
VK_9 39 57 9 keys
VK_A 41 65 A key
VK_B 42 66 B button
VK_C 43 67 C key
VK_D 44 68 D button
VK_E 45 69 E button
VK_F 46 70 F key
VK_G 47 71 G button
VK_H 48 72 H button
VK_I 49 73 I button
VK_J 4A 74 J key
VK_K 4B 75 K button
VK_L 4C 76 L key
VK_M 4D 77 M button
VK_N 4E 78 N button
VK_O 4F 79 O button
VK_P 50 80 P key
VK_Q 51 81 Q button
VK_R 52 82 R button
VK_S 53 83 S key
VK_T 54 84 T key
VK_U 55 85 U button
VK_V 56 86 V key
VK_W 57 87 W button
VK_X 58 88 X button
VK_Y 59 89 Y key
VK_Z 5A 90 Z button
VK_APPS 93 Right Ctrl left button, click equivalent to click the right mouse button, a shortcut
menu will pop up
186 ; (semicolon)
187 = key
191 / key
219 [key
220 / key
221 ] key
Shift: TShiftState);
Begin
Key:=0;
End;
Del
Begin
If not (Key in [‘0‘..‘9‘, #3, #22, #24, #8, #13, #45]) then Key := #0;
End;
Begin
Key:=#0;
End;
If, as you said by CKEN, you want to mask key events, you should actually assign them
directly:
Key:=0;
(Note that it is not Key:=#0, because the Key here is a Word type. In the keypress event, the
value of the key is Char, it is necessary to use "#")
Good luck
If key=vk_numpad0 then (If you are the 0 key of the keypad, do what you want to do)
VK_DECIMAL Decimal key (numeric keypad) VK_DIVIDE Divide key (numeric keypad)