首页 > 代码库 > [转]Delphi : keydown与keypress的区别,组合键
[转]Delphi : keydown与keypress的区别,组合键
Shift 是一个集合变量。type TShiftState = set of (ssShift, ssAlt, ssCtrl, ssLeft, ssRight, ssMiddle, ssDouble);也就是说TShiftstate类型有ssShift, ssAlt, ssCtrl, ssLeft(鼠标左键), ssRight(鼠标右键), ssMiddle(鼠标中键), ssDouble (鼠标双击)Value MeaningssShift The Shift key is held down.ssAlt The Alt key is held down.ssCtrl The Ctrl key is held down.ssLeft The left mouse button is held down.ssRight The right mouse button is held down.ssMiddle The middle mouse button is held down.ssDouble The mouse was double-clicked.用来判断组合键中是否用shift、Ctrl、鼠标左键、右键、中键、双击事件。 Delphi中一些常用的组合键值CTRL+A:#1 CTRL+B:#2 CTRL+C:#3 CTRL+D:#4 CTRL+E:#5 CTRL+F:#6 CTRL+G:#7CTRL+H:#8 CTRL+I:#9 CTRL+J:#10 CTRL+K:#11 CTRL+L:#12 CR:#13 CTRL+N:#14CTRL+O:#15 CTRL+P:#16 CTRL+Q:#17 CTRL+R:#18 CTRL+S:#19 CTRL+T:#20CTRL+U:#21 CTRL+V:#22 CTRL+W:#23 CTRL+X:#24 CTRL+Y:#25 CTRL+Z:#26 ESC:#27CTRL+:#28 CTRL+]:#29 SHIFT+CTRL+-:#30 DEL:#127 一般用在keydown、keyup、keypress事件中。OnKeyDown和OnKeyUp事件中的Key是Word型的,表示用户按的是键盘上的哪个键,OnKeyPress中的Key是Char型,表示用户输入什么字符。一个产生8位(char)常规健值,一个产生16位(word)功能健健值。1.KeyPress主要用来捕获数字(注意:包括Shift+数字的符号)、字母(注意:包括大小写)、小键盘等除了F1-12、SHIFT、Alt、Ctrl、Insert、Home、PgUp、Delete、End、PgDn、ScrollLock、Pause、NumLock、{菜单键}、{开始键}和方向键外的ANSI字符,KeyDown和KeyUp 通常可以捕获键盘除了PrScrn所有按键(这里不讨论特殊键盘的特殊键)2.KeyPress 只能捕获单个字符KeyDown 和KeyUp 可以捕获组合键。3.KeyPress 可以捕获单个字符的大小写4.KeyDown和KeyUp 对于单个字符捕获的KeyValue 都是一个值,也就是不能判断单个字符的大小写。5.KeyPress 不区分小键盘和主键盘的数字字符。KeyDown 和KeyUp 区分小键盘和主键盘的数字字符。6.其中PrScrn 按键KeyPress、KeyDown和KeyUp 都不能捕获。keydown是键盘按下任何键keypress是键盘按下任何数字键和任何字母键 keydown 与keypress的声明格式差别很大1 onkeydown事件type TKeyEvent = procedure (Sender: TObject; var Key: Word; Shift: TShiftState) of object;property OnKeyDown: TKeyEvent;该事件在有任何键按下的时候,就发生。看!它响应的是Word类型,还能识别Shift键状态2 onkeypress事件type TKeyPressEvent = procedure (Sender: TObject; var Key: Char) of object;property OnKeyPress: TKeyPressEvent;该事件响应的是字符类型Char,只能识别ASCII码,不能识别组合键和控制键和功能键。按下功能键、控制键不产生该事件,但产生OnkeyDown事件。 TWinControl与键盘相关的3个事件如下:OnKeyDown:键按下OnKeyPress:有Ascii字符产生OnKeyUp:键放开其中OnKeyDown、OnKeyUp对任何按键都有响应,传入参数为var Key: Word 键的扫描值 Shift: TShiftState 表示控制键状态,包括键盘上的Ctrl、Shift、Alt和鼠标的左、中、右键及双击。 OnKeyPress事件只对Ascii有响应,传入参数为var Key: Char Ascii字符//例如F1-F10只有onKeydown有反应另外TWincontrol. KeyPreview:Boolean属性确定控件是否在子控件之前处理按键。 比如说enter键,onkeypress事件if key=#13 then //当你按下enter键时触发该事件do something onkeydown事件if key=13 then //当你按下enter键时触发该事件do something 其中所有的KeyDown事件里面应该用如下判断:if Key = VK_RETURN then...所有的KeyPress事件里面应该用如下判断:if Key = #13 then...postmessage(self.handle,wm_keydown,vk_tab,0);keybd_event(VK_TAB,2,0,0);perform(WM_NEXTDLGCTL,0,0);SelectNext(ActiveControl, True, True);...但是显然用回车代码TAB显然不适合于BUTTON,你在它上面按回车的时候它会执行OnClick,所以不会TAB,你可以用别的键代替回车。 》》》》》》》》》》 键值 在keydown事件里面showmessage(inttostr(key));在keypress事件里面showmessage(key);就知道了。Form1.KeyPreview := True;procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;Shift: TShiftState);beginShowmessage(IntToStr(Key));end;procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);beginShowmessage(IntToStr(Ord(Key)));end;{ Virtual Keys, Standard Set }{$EXTERNALSYM VK_LBUTTON}VK_LBUTTON = 1;{$EXTERNALSYM VK_RBUTTON}VK_RBUTTON = 2;{$EXTERNALSYM VK_CANCEL}VK_CANCEL = 3;{$EXTERNALSYM VK_MBUTTON}VK_MBUTTON = 4; { NOT contiguous with L & RBUTTON }{$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) }{ VK_A thru VK_Z are the same as ASCII ’A’ thru ’Z’ ($41 - $5A) }{$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.Used only as parameters to GetAsyncKeyState() and GetKeyState().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;:),其实不用记,你不是用delphi吗,在ide里面输入vk_f1总会吧,然后按住ctrl键单击刚才你输入的vk_f1,就会出来啦!至于key:char的值,就是key:word的ascII码,你可以看chr(vk_f1),chr(vk_enter),都可以哈! 》》》》》》》》》》》》》》》》》》 delphi帮助里的虚拟键值表:vk_right right arrow keyvk_down down arrow keyvk_select select keyvk_print print key (keyboard-specific)vk_execute execute keyvk_snapshot print screen keyvk_insert insert keyvk_delete delete keyvk_help help keyvk_lwin left windows key (microsoft keyboard)vk_rwin right windows key (microsoft keyboard)vk_apps applications key (microsoft keyboard)vk_numpad0 0 key (numeric keypad)vk_numpad1 1 key (numeric keypad)vk_numpad2 2 key (numeric keypad)vk_numpad3 3 key (numeric keypad)vk_numpad4 4 key (numeric keypad)vk_numpad5 5 key (numeric keypad)vk_numpad6 6 key (numeric keypad)vk_numpad7 7 key (numeric keypad)vk_numpad8 8 key (numeric keypad)vk_numpad9 9 key (numeric keypad)vk_multiply multiply key (numeric keypad)vk_add add key (numeric keypad)vk_separator separator key (numeric keypad)vk_subtract subtract key (numeric keypad)vk_decimal decimal key (numeric keypad)vk_divide divide key (numeric keypad)vk_f1 f1 keyvk_f2 f2 keyvk_f3 f3 keyvk_f4 f4 keyvk_f5 f5 keyvk_f6 f6 keyvk_f7 f7 keyvk_f8 f8 keyvk_f9 f9 keyvk_f10 f10 keyvk_f11 f11 keyvk_f12 f12 keyvk_f13 f13 keyvk_f14 f14 keyvk_f15 f15 keyvk_f16 f16 keyvk_f17 f17 keyvk_f18 f18 keyvk_f19 f19 keyvk_f20 f20 keyvk_f21 f21 keyvk_f22 f22 keyvk_f23 f23 keyvk_f24 f24 keyvk_numlock num lock keyvk_scroll scroll lock keyvk_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)vk_processkey process keyvk_attn attn keyvk_crsel crsel keyvk_exsel exsel keyvk_ereof erase eof keyvk_play play keyvk_zoom zoom keyvk_noname reserved for future usevk_pa1 pa1 keyvk_oem_clear clear key 模拟鼠标鼠标移动(移动量dx,dy)mouse_event(MOUSEEVENTF_MOVE,dx,dy,0, 0 );鼠标在(x,y)处按下或释放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 );鼠标双击由两次按下与释放来模拟键盘模拟一般虚拟键值(VK_)由KeyDown或keyup事件得到特殊键值Tab--9Shift--16Ctrl--17Alt--18CapsLock--20Esc--27Win--91,92NumLock--144ScrollLock--145按下键keybd_event(key, MapVirtualKey(key, 0 ), KEYEVENTF_KEYUP , 0 );释放键keybd_event(key, MapVirtualKey(key, 0 ), 0 , 0 );以上做法对大多数键有效,少数键要用到不同的值 》》》》》》》》》》》》》》》》》》》》》》》》》 常数名称 十六进制值 十进制值 对应按键VK_LBUTTON 01 1 鼠标的左键VK_RBUTTON 02 2 鼠标的右键VK-CANCEL 03 3 Ctrl+Break(通常不需要处理)VK_MBUTTON 04 4 鼠标的中键(三按键鼠标)VK_BACK 08 8 Backspace键VK_TAB 09 9 Tab键VK_CLEAR 0C 12 Clear键(Num Lock关闭时的数字键盘5)VK_RETURN 0D 13 Enter键VK_SHIFT 10 16 Shift键VK_CONTROL 11 17 Ctrl键VK_MENU 12 18 Alt键VK_PAUSE 13 19 Pause键VK_CAPITAL 14 20 Caps Lock键VK_ESCAPE 1B 27 Ese键VK_SPACE 20 32 Spacebar键VK_PRIOR 21 33 Page Up键VK_NEXT 22 34 Page Domw键VK_END 23 35 End键VK_HOME 24 36 Home键VK_LEFT 25 37 LEFT ARROW 键(←)VK_UP 26 38 UP ARROW键(↑)VK_RIGHT 27 39 RIGHT ARROW键(→)VK_DOWN 28 40 DOWN ARROW键(↓)VK_Select 29 41 Select键VK_PRINT 2A 42 VK_EXECUTE 2B 43 EXECUTE键VK_SNAPSHOT 2C 44 Print Screen键(抓屏)VK_Insert 2D 45 Ins键(Num Lock关闭时的数字键盘0)VK_Delete 2E 46 Del键(Num Lock关闭时的数字键盘.)VK_HELP 2F 47 Help键VK_0 30 48 0键VK_1 31 49 1键VK_2 32 50 2键VK_3 33 51 3键VK_4 34 52 4键VK_5 35 53 5键VK_6 36 54 6键VK_7 37 55 7键VK_8 38 56 8键VK_9 39 57 9键VK_A 41 65 A键VK_B 42 66 B键VK_C 43 67 C键VK_D 44 68 D键VK_E 45 69 E键VK_F 46 70 F键VK_G 47 71 G键VK_H 48 72 H键VK_I 49 73 I键VK_J 4A 74 J键VK_K 4B 75 K键VK_L 4C 76 L键VK_M 4D 77 M键VK_N 4E 78 N键VK_O 4F 79 O键VK_P 50 80 P键VK_Q 51 81 Q键VK_R 52 82 R键VK_S 53 83 S键VK_T 54 84 T键VK_U 55 85 U键VK_V 56 86 V键VK_W 57 87 W键VK_X 58 88 X键VK_Y 59 89 Y键VK_Z 5A 90 Z键VK_NUMPAD0 60 96 数字键0键VK_NUMPAD1 61 97 数字键1键VK_NUMPAD2 62 98 数字键2键VK_NUMPAD3 62 99 数字键3键VK_NUMPAD4 64 100 数字键4键VK_NUMPAD5 65 101 数字键5键VK_NUMPAD6 66 102 数字键6键VK_NUMPAD7 67 103 数字键7键VK_NUMPAD8 68 104 数字键8键VK_NUMPAD9 69 105 数字键9键VK_MULTIPLY 6A 106 数字键盘上的*键VK_ADD 6B 107 数字键盘上的+键VK_SEPARATOR 6C 108 Separator键VK_SUBTRACT 6D 109 数字键盘上的-键VK_DECIMAL 6E 110 数字键盘上的.键VK_DIVIDE 6F 111 数字键盘上的/键VK_F1 70 112 F1键VK_F2 71 113 F2键VK_F3 72 114 F3键VK_F4 73 115 F4键VK_F5 74 116 F5键VK_F6 75 117 F6键VK_F7 76 118 F7键VK_F8 77 119 F8键VK_F9 78 120 F9键VK_F10 79 121 F10键VK_F11 7A 122 F11键VK_F12 7B 123 F12键VK_NUMLOCK 90 144 Num Lock 键VK_SCROLL 91 145 Scroll Lock键 上面没有提到的:(都在大键盘) VK_LWIN 91 左win键VK_RWIN 92 右win键VK_APPS 93 右Ctrl左边键,点击相当于点击鼠标右键,会弹出快捷菜单 186 ;(分号) 187 =键 188 ,键(逗号) 189 -键(减号) 190 .键(句号) 191 /键 192 `键(Esc下面) 219 [键 220 /键 221 ]键 222 ‘键(引号)procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if key=VK_DELETE then key:=0; end; Delphi6.0,2000下通过Top于在keydown中屏蔽掉除数字外的所有键还应该允许Ctr+C/X/V,所以总结起来应该是: procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); begin if not (Key in [‘0‘..‘9‘, #3, #22, #24, #8, #13, #45]) then Key := #0; end; procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); begin if (key>#57) and (key<#48) then key:=#0; end; 我想你最好将你的例程写出来。 如果是像CKEN阁下说的那样,你要将按键事件屏蔽的话,实际上应该是直接赋值: Key:=0; (注意不是Key:=#0,因为这里的Key是Word类型,在keypress事件中,key的值是Char型,就要用“#”) Good luck在KeyDown里面就可以实现 if key=vk_numpad0 then (如果为小键盘的0键就干你想干的事情) 下面是小键盘的值 VK_NUMPAD0 0 key (numeric keypad) VK_NUMPAD1 1 key (numeric keypad) VK_NUMPAD2 2 key (numeric keypad) VK_NUMPAD3 3 key (numeric keypad) VK_NUMPAD4 4 key (numeric keypad) VK_NUMPAD5 5 key (numeric keypad) VK_NUMPAD6 6 key (numeric keypad) VK_NUMPAD7 7 key (numeric keypad) VK_NUMPAD8 8 key (numeric keypad) VK_NUMPAD9 9 key (numeric keypad) VK_MULTIPLY Multiply key (numeric keypad) VK_ADD Add key (numeric keypad) VK_SEPARATOR Separator key (numeric keypad) VK_SUBTRACT Subtract key (numeric keypad) VK_DECIMAL Decimal key (numeric keypad) VK_DIVIDE Divide key (numeric keypad) 》》》》》》》》》》》》》》》
[转]Delphi : keydown与keypress的区别,组合键
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。