1. 程式人生 > >再談vc傳送鍵盤、組合鍵訊息

再談vc傳送鍵盤、組合鍵訊息

關於向Windows視窗傳送Alt組合鍵的問題,這個真是經典問題啊,在網上找了一下,問的人N多,方法差不多,

但就是沒有很好解決問題。

之前找到一個能正確傳送的code:(Alt+A)

PostMessage(hWnd,WM_SYSKEYDOWN,VK_MENU,0);

PostMessage(hWnd,WM_SYSKEYDOWN,0x41,0);

Sleep(50);

PostMessage(hWnd,WM_SYSKEYUP,0x41,0);

PostMessage(hWnd,WM_SYSKEYUP,VK_MENU,0);

有人解釋說,按下組合鍵的時候系統是發兩條訊息的

但是看到Win32 SDK,感覺上就發一次就可以了……

偶然間又看到最後一個引數的說明,有所發現!先看WM_SYSKEYDOWN的help

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.

WM_SYSKEYDOWN  
nVirtKey = (int) wParam; // virtual-key code
lKeyData = lParam;       // key data


Parameters

nVirtKey

Value of wParam. Specifies the virtual-key code of the key being pressed.

lKeyData

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:

Value Description
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.
16-23 Specifies the scan code. The value depends on the original equipment manufacturer (OEM).
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.
25-28 Reserved; do not use.
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.
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.
31 Specifies the transition state. The value is always 0 for a WM_SYSKEYDOWN message.


之前曾經修改過keyData的16-23位為VK_MENU,第30位引數為1,但沒效果

請看位29的說明!!

The value is 1 if the ALT key is down while the key is pressed;

當值為1時表示ALT鍵被按下!這不正是我需要的嗎?於是把29位設定為1,函式呼叫變成

PostMessage(hWnd,WM_SYSKEYDOWN,0x41,1<<29);

經過測試,發現這個就是Alt+A的效果!!原來這麼簡單,但為什麼很多人弄得那麼複雜,我當時查詢的時候也是迷惘啊,浪費了N多小時。

類似有個WM_SYSKEYUP,WM_SYSCHAR(這個不知道幹什麼用)

記錄一下免得 又便成了 找不到資料的傻子了

postmessage(edit1.handle,wm_keydown,vk_down,$20000000)

Ctrl : $10000000;  
Shift: $08000000;  
Alt:20000000

Delphi鍵盤按鍵偽碼

if key = chr(VK_RETURN) then...

常數名稱 十六進位制值 十進位制值 對應按鍵
VK_LBUTTON 01 1 滑鼠的左鍵
VK_RBUTTON 02 2 滑鼠的右鍵
VK-CANCEL 03 3 Contol-break 執行
VK_MBUTTON 04 4 滑鼠的中鍵(三按鍵滑鼠)
VK_BACK 08 8 Backspace鍵
VK_TAB 09 9 Tab鍵
VK_CLEAR 0C 12 Clear鍵
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_EXECUTE 2B 43 EXECUTE鍵
VK_SNAPSHOT 2C 44 Print Screen鍵
VK_INSERT 2D 45 Ins鍵
VK_DELETE 2E 46 Del鍵
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_BZ 5A 90 Z鍵
VK_NUMPAD0 60 96 數字鍵0鍵
VK_NUMPAD1 61 97 數字鍵1鍵
VK_NUMPAD2 62 98 數字鍵2鍵
VK_NUMPAD3 63 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鍵