PostMessage 问题,求助!!!(200分)

  • 主题发起人 主题发起人 小小小虾米
  • 开始时间 开始时间

小小小虾米

Unregistered / Unconfirmed
GUEST, unregistred user!
PostMessage 如何向Windows窗口发送CTRL+V组合键 ,<br>我在网上找了很久都没找到解决办法;有一个PostMessage 向Windows窗口发送Alt组合键 的,但用在CTRL+V却不成功!那位大侠能帮帮忙!
 
PostMessage 向Windows窗口发送Alt组合键 关于向Windows窗口发送Alt组合键的问题,这个真是经典问题啊,在网上找了一下,问的人N多,方法差不多,<br><br>但就是没有很好解决问题。<br><br>之前找到一个能正确发送的code:(Alt+A)<br><br>PostMessage(hWnd,WM_SYSKEYDOWN,VK_MENU,0);<br><br>PostMessage(hWnd,WM_SYSKEYDOWN,0x41,0);<br><br>Sleep(50);<br><br>PostMessage(hWnd,WM_SYSKEYUP,0x41,0);<br><br>PostMessage(hWnd,WM_SYSKEYUP,VK_MENU,0);<br><br>有人解释说,按下组合键的时候系统是发两条消息的<br><br>但是看到Win32 SDK,感觉上就发一次就可以了……<br><br>偶然间又看到最后一个参数的说明,有所发现!先看WM_SYSKEYDOWN的help<br><br>The WM_SYSKEYDOWN message is posted to the window with the keyboard focus when the user holds down the ALT key and then presses another key. It also occurs when no window currently has the keyboard focus; in this case, the WM_SYSKEYDOWN message is sent to the active window. The window that receives the message can distinguish between these two contexts by checking the context code in the lKeyData parameter. <br><br>WM_SYSKEYDOWN &nbsp;<br>nVirtKey = (int) wParam; // virtual-key code <br>lKeyData = lParam; &nbsp; &nbsp; &nbsp; // key data <br><br><br>Parameters<br><br>nVirtKey<br><br>Value of wParam. Specifies the virtual-key code of the key being pressed. <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 1 if the ALT key is down while the key is pressed; it is 0 if the WM_SYSKEYDOWN message is posted to the active window because no window has the keyboard focus.<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_SYSKEYDOWN message.<br><br><br>之前曾经修改过keyData的16-23位为VK_MENU,第30位参数为1,但没效果<br><br>请看位29的说明!!<br><br>The value is 1 if the ALT key is down while the key is pressed; <br><br>当值为1时表示ALT键被按下!这不正是我需要的吗?于是把29位设置为1,函数调用变成<br><br>PostMessage(hWnd,WM_SYSKEYDOWN,0x41,1&lt;&lt;29);<br><br>经过测试,发现这个就是Alt+A的效果!!原来这么简单,但为什么很多人弄得那么复杂,我当时查找的时候也是迷惘啊,浪费了N多小时。<br><br>类似有个WM_SYSKEYUP,WM_SYSCHAR(这个不知道干什么用)<br><br><br>记录一下免得 又便成了 找不到资料的傻子了<br><br>postmessage(edit1.handle,wm_keydown,vk_down,$20000000)<br><br>Ctrl : $10000000; &nbsp; <br>Shift: $08000000; &nbsp; <br>Alt:20000000<br><br><br>Delphi键盘按键伪码<br><br>if key = chr(VK_RETURN) then...<br><br>常数名称 十六进制值 十进制值 对应按键<br>VK_LBUTTON 01 1 鼠标的左键<br>VK_RBUTTON 02 2 鼠标的右键<br>VK-CANCEL 03 3 Contol-break 执行<br>VK_MBUTTON 04 4 鼠标的中键(三按键鼠标)<br>VK_BACK 08 8 Backspace键<br>VK_TAB 09 9 Tab键<br>VK_CLEAR 0C 12 Clear键<br>VK_RETURN 0D 13 Enter键<br>VK_SHIFT 10 16 Shift键<br>VK_CONTROL 11 17 Ctrl键<br>VK_MENU 12 18 Alt键<br>VK_PAUSE 13 19 Pause键<br>VK_CAPITAL 14 20 Caps Lock键<br>VK_ESCAPE 1B 27 Ese键<br>VK_SPACE 20 32 Spacebar键<br>VK_PRIOR 21 33 Page Up键<br>VK_NEXT 22 34 Page Domw键<br>VK_END 23 35 End键<br>VK_HOME 24 36 Home键<br>VK_LEFT 25 37 LEFT ARROW 键(←)<br>VK_UP 26 38 UP ARROW键(↑)<br>VK_RIGHT 27 39 RIGHT ARROW键(→)<br>VK_DOWN 28 40 DOWN ARROW键(↓)<br>VK_SELECT 29 41 SELECT键<br>VK_EXECUTE 2B 43 EXECUTE键<br>VK_SNAPSHOT 2C 44 Print Screen键 <br>VK_INSERT 2D 45 Ins键<br>VK_DELETE 2E 46 Del键<br>VK_HELP 2F 47 Help键<br>VK_0 30 48 0键<br>VK_1 31 49 1键<br>VK_2 32 50 2键<br>VK_3 33 51 3键<br>VK_4 34 52 4键<br>VK_5 35 53 5键<br>VK_6 36 54 6键<br>VK_7 37 55 7键<br>VK_8 38 56 8键<br>VK_9 39 57 9键<br>VK_A 41 65 A键<br>VK_B 42 66 B键<br>VK_C 43 67 C键<br>VK_D 44 68 D键<br>VK_E 45 69 E键<br>VK_F 46 70 F键<br>VK_G 47 71 G键<br>VK_H 48 72 H键<br>VK_I 49 73 I键<br>VK_J 4A 74 J键<br>VK_K 4B 75 K键<br>VK_L 4C 76 L键<br>VK_M 4D 77 M键<br>VK_N 4E 78 N键<br>VK_O 4F 79 O键<br>VK_P 50 80 P键<br>VK_Q 51 81 Q键<br>VK_R 52 82 R键<br>VK_S 53 83 S键<br>VK_T 54 84 T键<br>VK_U 55 85 U键<br>VK_V 56 86 V键<br>VK_W 57 87 W键<br>VK_X 58 88 X键<br>VK_Y 59 89 Y键<br>VK_BZ 5A 90 Z键<br>VK_NUMPAD0 60 96 数字键0键<br>VK_NUMPAD1 61 97 数字键1键<br>VK_NUMPAD2 62 98 数字键2键<br>VK_NUMPAD3 63 99 数字键3键<br>VK_NUMPAD4 64 100 数字键4键<br>VK_NUMPAD5 65 101 数字键5键<br>VK_NUMPAD6 66 102 数字键6键<br>VK_NUMPAD7 67 103 数字键7键<br>VK_NUMPAD8 68 104 数字键8键<br>VK_NUMPAD9 69 105 数字键9键<br>VK_MULTIPLY 6A 106 *键<br>VK_ADD 6B 107 +键<br>VK_SEPARATOR 6C 108 Separator键<br>VK_SUBTRACT 6D 109 -键<br>VK_DECIMAL 6E 110 .键<br>VK_DIVIDE 6F 111 键<br>VK_F1 70 112 F1键<br>VK_F2 71 113 F2键<br>VK_F3 72 114 F3键<br>VK_F4 73 115 F4键<br>VK_F5 74 116 F5键<br>VK_F6 75 117 F6键<br>VK_F7 76 118 F7键<br>VK_F8 77 119 F8键<br>VK_F9 78 120 F9键<br>VK_F10 79 121 F10键<br>VK_F11 7A 122 F11键<br>VK_F12 7B 123 F12键<br>VK_NUMLOCK 90 144 Num Lock 键<br>VK_SCROLL 91 145 Scroll Lock键
 
上面那个是我在网上找到的.
 
没人会啊~~~~~
 
喊有Alt,Ctrl的,好象Message方式不行,只能把窗口即活为单前的,然后再<br>KeyboardEventWM_SYSKEYDOWN,VK_MENU,0);<br>KeyboardEvent(WM_SYSKEYDOWN,0x41,0);<br>Sleep(50);<br>KeyboardEvent(WM_SYSKEYUP,0x41,0);<br>KeyboardEvent(WM_SYSKEYUP,VK_MENU,0);<br>因为Alt,Ctrl等是热件即活键,属于窗口Command系列,所以不是即活窗口,<br>一般不会起作用!
 
to: 小小小虾米,你说的那个发送ALT+A的那能用吗,试过了吗.我测试的不行啊.<br>PostMessage(hWnd,WM_SYSKEYDOWN,0x41,1&lt;&lt;29);<br>中的的0x41,在delphi中好像不行吧.得改成65,<br>&lt;&lt; 这个操作在delphi 中好像也不行,得改成shl <br>PostMessage(hWnd,WM_SYSKEYDOWN,65,1 shl 29);<br>我测试不成功.,另外;<br>PostMessage(hWnd,WM_SYSKEYDOWN,VK_MENU,0);<br><br>PostMessage(hWnd,WM_SYSKEYDOWN,0x41,0);<br><br>Sleep(50);<br><br>PostMessage(hWnd,WM_SYSKEYUP,0x41,0);<br><br>PostMessage(hWnd,WM_SYSKEYUP,VK_MENU,0);<br><br>也不成功.
 
请问如何用postmessage向一个wnd发送一个Ctrl+A的消息<br>楼主SnowyWolf(小马哥)2002-12-03 19:16:16 在 VC/MFC / 基础类 提问<br>请问如何用postmessage向一个wnd发送一个Ctrl+A的消息 &nbsp; <br>&nbsp; &nbsp; <br>1 楼JennyVenus()回复于 2002-12-03 19:19:27 得分 100<br>CTRL &nbsp; + &nbsp; C &nbsp; <br>&nbsp; WPARAM &nbsp; wParam &nbsp; = &nbsp; (WPARAM)'C'; &nbsp; <br>&nbsp; LPARAM &nbsp; lParam &nbsp; = &nbsp; 0x412e0001; &nbsp; //01000001 &nbsp; 00101110 &nbsp; 00000000 &nbsp; 00000001 &nbsp; <br>&nbsp; 其中00101110 &nbsp; = &nbsp; 0x2e &nbsp; 表示Ctrl键 &nbsp; <br>&nbsp; SendMessage(hwnd, &nbsp; WM_KEYDOWN,wParam, &nbsp; lParam); &nbsp; &nbsp; <br>&nbsp; 实际上最简单的方法可以通过VC的工具SPY++来查看。 &nbsp; <br>&nbsp; ctrl+c是通过两次WM_KEYDOWN消息发送的,第一次发送nVirtKey是VK_CONTROL,ScanCode为1D,第二次发送nVirtKey是'C',ScanCode是2E。 &nbsp; <br>&nbsp; spy++显示,如果要完整模拟Ctrl+C的按键过程,应该发送5次消息,如下: &nbsp; <br>&nbsp; 1:WM_KEYDOWN &nbsp; nVirtKey:VK_CONTROL &nbsp; cRepeat:1 &nbsp; ScanCode:1D &nbsp; fExtended:0 &nbsp; fAltDown:0 &nbsp; fRepeat:0 &nbsp; fUp:0 &nbsp; <br>&nbsp; 2:WM_KEYDOWN &nbsp; nVirtKey:'C' &nbsp; cRepeat:1 &nbsp; ScanCode:2E &nbsp; fExtended:0 &nbsp; fAltDown:0 &nbsp; fRepeat:0 &nbsp; fUp:0 &nbsp; <br>&nbsp; 3:WM_CHAR &nbsp; chCharCode:'' &nbsp; (3) &nbsp; cRepeat:1 &nbsp; ScanCode:2E &nbsp; fExtended:0 &nbsp; fAltDown:0 &nbsp; fRepeat:0 &nbsp; fUp:0 &nbsp; <br>&nbsp; 4:WM_KEYUP &nbsp; nVirtKey:'C' &nbsp; cRepeat:1 &nbsp; ScanCode:2E &nbsp; fExtended:0 &nbsp; fAltDown:0 &nbsp; fRepeat:1 &nbsp; fUp:1 &nbsp; <br>&nbsp; 5:WM_KEYUP &nbsp; nVirtKey:VK_CONTROL &nbsp; cRepeat:1 &nbsp; ScanCode:1D &nbsp; fExtended:0 &nbsp; fAltDown:0 &nbsp; fRepeat:1 &nbsp; fUp:1 &nbsp; <br><br>2 楼JennyVenus()回复于 2002-12-03 19:19:46 得分 0 <br>CTRL &nbsp; + &nbsp; A &nbsp; <br>&nbsp; WPARAM &nbsp; wParam &nbsp; = &nbsp; (WPARAM)'A'; &nbsp; <br>&nbsp; LPARAM &nbsp; lParam &nbsp; = &nbsp; 0x412e0001; &nbsp; //01000001 &nbsp; 00101110 &nbsp; 00000000 &nbsp; 00000001 &nbsp; <br>&nbsp; 其中00101110 &nbsp; = &nbsp; 0x2e &nbsp; 表示Ctrl键 &nbsp; <br>&nbsp; SendMessage(hwnd, &nbsp; WM_KEYDOWN,wParam, &nbsp; lParam); &nbsp; &nbsp; <br>&nbsp; 实际上最简单的方法可以通过VC的工具SPY++来查看。 &nbsp; <br>&nbsp; ctrl+c是通过两次WM_KEYDOWN消息发送的,第一次发送nVirtKey是VK_CONTROL,ScanCode为1D,第二次发送nVirtKey是'C',ScanCode是2E。 &nbsp; <br>&nbsp; spy++显示,如果要完整模拟Ctrl+C的按键过程,应该发送5次消息,如下: &nbsp; <br>&nbsp; 1:WM_KEYDOWN &nbsp; nVirtKey:VK_CONTROL &nbsp; cRepeat:1 &nbsp; ScanCode:1D &nbsp; fExtended:0 &nbsp; fAltDown:0 &nbsp; fRepeat:0 &nbsp; fUp:0 &nbsp; <br>&nbsp; 2:WM_KEYDOWN &nbsp; nVirtKey:'C' &nbsp; cRepeat:1 &nbsp; ScanCode:2E &nbsp; fExtended:0 &nbsp; fAltDown:0 &nbsp; fRepeat:0 &nbsp; fUp:0 &nbsp; <br>&nbsp; 3:WM_CHAR &nbsp; chCharCode:'' &nbsp; (3) &nbsp; cRepeat:1 &nbsp; ScanCode:2E &nbsp; fExtended:0 &nbsp; fAltDown:0 &nbsp; fRepeat:0 &nbsp; fUp:0 &nbsp; <br>&nbsp; 4:WM_KEYUP &nbsp; nVirtKey:'C' &nbsp; cRepeat:1 &nbsp; ScanCode:2E &nbsp; fExtended:0 &nbsp; fAltDown:0 &nbsp; fRepeat:1 &nbsp; fUp:1 &nbsp; <br>&nbsp; 5:WM_KEYUP &nbsp; nVirtKey:VK_CONTROL &nbsp; cRepeat:1 &nbsp; ScanCode:1D &nbsp; fExtended:0 &nbsp; fAltDown:0 &nbsp; fRepeat:1 &nbsp; fUp:1
 
谁能帮忙将上面的代码翻译成Delphi的,谢谢!
 
对memo1按下CTRL+V的代码<br><br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; keyboardState: TKeyBoardState;<br>begin<br>&nbsp; keyboardState[17] := 129;<br>&nbsp; SetKeyBoardState(keyboardState);<br>&nbsp; PostMessage(memo1.Handle, 256, 86, 1);<br>&nbsp; Application.ProcessMessages;<br>&nbsp; keyboardState[17] := 0;<br>&nbsp; SetKeyBoardState(keyboardState);<br>end;<br><br>成功的
 

Similar threads

S
回复
0
查看
816
SUNSTONE的Delphi笔记
S
S
回复
0
查看
735
SUNSTONE的Delphi笔记
S
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部