怎样模拟键盘输入组合键?(100分)

  • 主题发起人 主题发起人 DreamTiger
  • 开始时间 开始时间
D

DreamTiger

Unregistered / Unconfirmed
GUEST, unregistred user!
想对Memo模拟输入Shift+Ctrl+I键,不知该怎么做?<br>Memo1.Preform(WM_KEYDOWN,WParam,lParam)<br>的WParam应该怎么写?
 
用SendMessage我觉的很难,WM_KeyDown的WParam太复杂了,用下面的吧<br>&nbsp; keybd_event( VK_MENU, MapVirtualKey( VK_MENU, 0 ), 0 , 0 ); &nbsp; &nbsp; // Alt down <br>&nbsp; keybd_event( VK_DOWN, MapVirtualKey( VK_DOWN, 0 ), 0 , 0 ); &nbsp; &nbsp; // down arrow key down <br>&nbsp; keybd_event( VK_DOWN, MapVirtualKey( VK_DOWN, 0 ), KEYEVENTF_KEYUP , 0 ); &nbsp;// down arrow key up <br>&nbsp; keybd_event( VK_MENU, MapVirtualKey( VK_MENU, 0 ), KEYEVENTF_KEYUP , 0 ); &nbsp;// Alt key up <br>经本人试验,确实可行,另加补充:<br>&nbsp; &nbsp;Ctrl: VK_CONTROL<br>&nbsp; &nbsp;SHIFT:VK_SHIFT<br>&nbsp; &nbsp;TAB: &nbsp;VK_TAB<br>&nbsp; &nbsp;'A': &nbsp;byte('A') &nbsp;<br>
 
我的程序中组合键是一个控件在KeyDown 中监视的,我现在需要<br>在单击某个菜单后,对这个控件进行模拟输入这个组合键。同时<br>为了让用户知道有这个功能,把这个组合键作为了这个菜单的快<br>捷键。我想要做的就是在这个菜单项的执行中对这个控件进行模<br>拟输入Shift+Ctrl+I。<br><br>Nose:你的方法我试了,不行。<br>
 
本月第15名你好,第16名向你问候!咱两能挨着也是缘分,xixi.<br><br>TWinControl有KeyDown方法,是动态的.TEdit,TMemo等控件没有继承下来,<br>但是TForm却有.使用很方便的,只需要指明key和shift.<br>如果在memo1中监视组合键,可以这样:<br>1.设置Form的KeyPreview:=true;<br>2.Form的OnKeyDown事件:<br>procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;Shift: TShiftState);<br>begin<br>&nbsp; &nbsp; &nbsp;if (shift=[ssShift,ssCtrl]) and (Key=$49) then<br>&nbsp; &nbsp; &nbsp; &nbsp; memo1.OnKeyDown(Sender,Key,[ssShift,ssCtrl]);<br>end;<br>3.我这里用按钮模拟输入组合键:<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; &nbsp;mykey:word;<br>begin<br>&nbsp; &nbsp; &nbsp;mykey:=$49;<br>&nbsp; &nbsp; &nbsp;form1.KeyDown(mykey,[ssCtrl,ssShift]);<br>end;<br>4.下面就是memo1中的OnKeyDown了:<br>procedure TForm1.Memo1KeyDown(Sender: TObject; var Key: Word;<br>&nbsp; Shift: TShiftState);<br>begin<br>&nbsp; &nbsp; &nbsp;if (shift=[ssShift,ssCtrl]) and (key=$49) then<br>&nbsp; &nbsp; &nbsp; &nbsp; showmessage('Ctrl+Shift+I pressed');<br>end;<br><br>已经试验通过
 
咱是无名只能同意第16名的:)
 
哎,真是"脱裤子放屁,多费手续"...:-))<br>其实能更简单的:<br>1.<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; &nbsp;key:word;<br>begin<br>&nbsp; &nbsp; &nbsp;key:=$49;<br>&nbsp; &nbsp; &nbsp;memo1.OnKeyDown(memo1,key,[ssctrl,ssshift]);<br>end;<br>2.<br>procedure TForm1.Memo1KeyDown(Sender: TObject; var Key: Word;<br>&nbsp; Shift: TShiftState);<br>begin<br>&nbsp; &nbsp; &nbsp;if (key=$49) and (Shift=[ssctrl,ssshift]) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;showmessage('Ctrl+Shift+I pressed');<br>end;<br><br>什么keypreview不用管它的.
 
popeye:<br>&nbsp; 呵呵,如果这么简单我就不用问了。:(<br>&nbsp; 问题就是memo1的调用不是在OnKeyDown中,它是一个私有函数,监视了<br>键盘,也就是说是KeyDown,而不是OnKeyDown。我不想把控件的私有函数<br>公有化,只能对它模拟键盘输入了。<br><br>Nose:<br>&nbsp; 能不能讲讲这个复杂的WM_KeyDown的WParam?
 
WM_KEYDOWN &nbsp;<br>nVirtKey = (int) wParam; &nbsp; &nbsp;// virtual-key code wParam中放的就是键值。<br>lKeyData = lParam; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// key data lParam中记录了一些有关的数据,如击键次数,扫描码什么的。<br><br>Parameters<br><br>nVirtKey<br><br>Value of wParam. Specifies the virtual-key code of the nonsystem key. <br><br>lKeyData<br><br>Value of lParam. Specifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag, as shown in the following table: <br><br>Value Description<br>0-15 Specifies the repeat count. The value is the number of times the keystroke is repeated as a result of the user holding down the key.<br>16-23 Specifies the scan code. The value depends on the original equipment manufacturer (OEM).<br>24 Specifies whether the key is an extended key, such as the right-hand ALT and CTRL keys that appear on an enhanced 101- or 102-key keyboard. The value is 1 if it is an extended key; otherwise, it is 0.<br>25-28 Reserved; do not use.<br>29 Specifies the context code. The value is always 0 for a WM_KEYDOWN message.<br>30 Specifies the previous key state. The value is 1 if the key is down before the message is sent, or it is 0 if the key is up.<br>31 Specifies the transition state. The value is always 0 for a WM_KEYDOWN message.<br><br><br><br>键值表:<br>Symbolic constant name Value (hexadecimal) Mouse or keyboard equivalent<br>VK_LBUTTON 01 Left mouse button <br>VK_RBUTTON 02 Right mouse button <br>VK_CANCEL 03 Control-break processing <br>VK_MBUTTON 04 Middle mouse button (three-button mouse) <br>&nbsp;? 05-07 Undefined <br>VK_BACK 08 BACKSPACE key <br>VK_TAB 09 TAB key <br>&nbsp;? 0A-0B Undefined <br>VK_CLEAR 0C CLEAR key <br>VK_RETURN 0D ENTER key <br>&nbsp;? 0E-0F Undefined <br>VK_SHIFT 10 SHIFT key <br>VK_CONTROL 11 CTRL key <br>VK_MENU 12 ALT key <br>VK_PAUSE 13 PAUSE key <br>VK_CAPITAL 14 CAPS LOCK key <br>&nbsp;? 15-19 Reserved for Kanji systems <br>&nbsp;? 1A Undefined <br>VK_ESCAPE 1B ESC key <br>&nbsp;? 1C-1F Reserved for Kanji systems <br>VK_SPACE 20 SPACEBAR <br>VK_PRIOR 21 PAGE UP key <br>VK_NEXT 22 PAGE DOWN key <br>VK_END 23 END key <br>VK_HOME 24 HOME key <br>VK_LEFT 25 LEFT ARROW key <br>VK_UP 26 UP ARROW key <br>VK_RIGHT 27 RIGHT ARROW key <br>VK_DOWN 28 DOWN ARROW key <br>VK_SELECT 29 SELECT key <br>&nbsp;? 2A Original equipment manufacturer (OEM) specific <br>VK_EXECUTE 2B EXECUTE key <br>VK_SNAPSHOT 2C PRINT SCREEN key for Windows 3.0 and later <br>VK_INSERT 2D INS key <br>VK_DELETE 2E DEL key <br>VK_HELP 2F HELP key <br>VK_0 30 0 key <br>VK_1 31 1 key <br>VK_2 32 2 key <br>VK_3 33 3 key <br>VK_4 34 4 key <br>VK_5 35 5 key <br>VK_6 36 6 key <br>VK_7 37 7 key <br>VK_8 38 8 key <br>VK_9 39 9 key <br>&nbsp;? 3A-40 Undefined <br>VK_A 41 A key <br>VK_B 42 B key <br>VK_C 43 C key <br>VK_D 44 D key <br>VK_E 45 E key <br>VK_F 46 F key <br>VK_G 47 G key <br>VK_H 48 H key <br>VK_I 49 I key <br>VK_J 4A J key <br>VK_K 4B K key <br>VK_L 4C L key <br>VK_M 4D M key <br>VK_N 4E N key <br>VK_O 4F O key <br>VK_P 50 P key <br>VK_Q 51 Q key <br>VK_R 52 R key <br>VK_S 53 S key <br>VK_T 54 T key <br>VK_U 55 U key <br>VK_V 56 V key <br>VK_W 57 W key <br>VK_X 58 X key <br>VK_Y 59 Y key <br>VK_Z 5A Z key <br>VK_LWIN 5B Left Windows key (Microsoft Natural Keyboard) <br>VK_RWIN 5C Right Windows key (Microsoft Natural Keyboard) <br>VK_APPS 5D Applications key (Microsoft Natural Keyboard) <br>&nbsp;? 5E-5F Undefined <br>VK_NUMPAD0 60 Numeric keypad 0 key <br>VK_NUMPAD1 61 Numeric keypad 1 key <br>VK_NUMPAD2 62 Numeric keypad 2 key <br>VK_NUMPAD3 63 Numeric keypad 3 key <br>VK_NUMPAD4 64 Numeric keypad 4 key <br>VK_NUMPAD5 65 Numeric keypad 5 key <br>VK_NUMPAD6 66 Numeric keypad 6 key <br>VK_NUMPAD7 67 Numeric keypad 7 key <br>VK_NUMPAD8 68 Numeric keypad 8 key <br>VK_NUMPAD9 69 Numeric keypad 9 key <br>VK_MULTIPLY 6A Multiply key <br>VK_ADD 6B Add key <br>VK_SEPARATOR 6C Separator key <br>VK_SUBTRACT 6D Subtract key <br>VK_DECIMAL 6E Decimal key <br>VK_DIVIDE 6F Divide key <br>VK_F1 70 F1 key <br>VK_F2 71 F2 key <br>VK_F3 72 F3 key <br>VK_F4 73 F4 key <br>VK_F5 74 F5 key <br>VK_F6 75 F6 key <br>VK_F7 76 F7 key <br>VK_F8 77 F8 key <br>VK_F9 78 F9 key <br>VK_F10 79 F10 key <br>VK_F11 7A F11 key <br>VK_F12 7B F12 key <br>VK_F13 7C F13 key <br>VK_F14 7D F14 key <br>VK_F15 7E F15 key <br>VK_F16 7F F16 key <br>VK_F17 80H F17 key <br>VK_F18 81H F18 key <br>VK_F19 82H F19 key <br>VK_F20 83H F20 key <br>VK_F21 84H F21 key <br>VK_F22 85H F22 key <br>VK_F23 86H F23 key <br>VK_F24 87H F24 key <br>&nbsp;? 88-8F Unassigned <br>VK_NUMLOCK 90 NUM LOCK key <br>VK_SCROLL 91 SCROLL LOCK key <br>&nbsp;? 92-B9 Unassigned <br>&nbsp;? BA-C0 OEM specific <br>&nbsp;? C1-DA Unassigned <br>&nbsp;? DB-E4 OEM specific <br>&nbsp;? E5 Unassigned <br>&nbsp;? E6 OEM specific <br>&nbsp;? E7-E8 Unassigned <br>&nbsp;? E9-F5 OEM specific <br>VK_ATTN F6 Attn key<br>VK_CRSEL F7 CrSel key<br>VK_EXSEL F8 ExSel key<br>VK_EREOF F9 Erase EOF key<br>VK_PLAY FA Play key<br>VK_ZOOM FB Zoom key<br>VK_NONAME FC Reserved for future use. <br>VK_PA1 FD PA1 key<br>VK_OEM_CLEAR FE Clear key
 
WM_KEYDOWN 的wParam很简单,就是虚键值.lParam比较烦.<br>lParam是一个long integer,占用两个字节,共32bit.帮助上讲:<br>0-15 &nbsp;bit:击键的重复率.在键盘上按下一个键时由于会持续一段时间,所以键盘会<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 发出多次的信号.计算机在接受信号时会使用抖动方法确认是一次击键.<br>16-23 bit:扫描码<br>24 &nbsp; &nbsp;bit:如果是extend key,=1<br>25-28 bit:保留<br>29 &nbsp; &nbsp;bit:对于keydown消息总是0<br>30 &nbsp; &nbsp;bit:前一个键的状态,如果在发送消息之前已经按下,=1<br>31 &nbsp; &nbsp;bit:对于keydown消息总是0<br>我试了一下,i键的lParam按以上方法设:<br>memo1.perform(WM_KEYDOWN,$49,$40170008);可以检测到<br>BTW:VK_I编译不能通过,为什么?<br>memo1.perform(WM_KEYDOWN,vk_control,$01230008);<br>但是ctrl,shift就监测不到.我还看了一下keybd_event传过来的lParam,是1.<br>我改成memo1.perform(WM_KEYDOWN,vk_control,1);也不行.<br><br>keybd_event可行.不过要先把焦点转移到memo1.<br>memo1.setfocus;<br>keybd_event(vk_control,0,0,0);<br>keybd_event(vk_shift,0,0,0);<br>keybd_event($49,0,0,0);<br>keybd_event($49,0,KEYEVENTF_KEYUP,0);<br>keybd_event(vk_shift,0,KEYEVENTF_KEYUP,0);<br>keybd_event(vk_control,0,KEYEVENTF_KEYUP,0);<br><br>
 
多人接受答案了。
 
后退
顶部