最近發現個有趣的問題,我們如何向乙個視窗傳送 按鍵指令?
嗯,我稍微總結了下,大概有如下幾種:
1:system.windows.forms.sendkeys. send / sendwait
示例:sendkeys.sendwait("^"); // 模擬鍵:ctrl + a
組合鍵:
ctrl = ^
shift = +
alt = %
2:keybd_event
宣告:[system.runtime.interopservices.dllimportattribute("user32.dll", entrypoint="keybd_event")]
public static extern void keybd_event(byte bvk, byte bscan, uint dwflags, uint dwextrainfo) ;
示列:keybd_event (vk_control, 0, 0, 0);
keybd_event (vk_a, 0, 0, 0);
keybd_event (vk_a, 0, #keyeventf_keyup, 0);
keybd_event (vk_control, 0, keyeventf_keyup, 0); // 模擬鍵:ctrl + a
看了兩大兩種,但又沒有對單一視窗,即使不是活動視窗也能投遞按鍵訊息呢?
答案肯定是有的,我稍微整合了一點,如下寫到
宣告:[system.runtime.interopservices.dllimportattribute("user32.dll", entrypoint = "postmessagea")]
static extern int postmessagea(intptr hwnd, int msg, int wparam, int lparam);
1:向 textbox 傳送單一按鍵,這個比較簡單我們直接這樣寫就可以了
postmessagea(textbox1.handle, wm_char, vk_a, 1);
詳解:2:按下某鍵,並已發出wm_keydown, wm_keyup訊息
3:vk_a = (int)keys.a = ascii(065)
4:傳送字元數量,0和1 一樣只傳送一次
2:向(任意視窗)投遞 ctrl + a 組合鍵
案例:keybd_event (vk_control, 0, 0, 0);
postmessage (hwnd, #wm_keydown, #vk_a, 0);
keybd_event (vk_control, 0, keyeventf_keyup, 0);
// 不過也不是好完美了那,誰讓本吊絲沒把postmessagea怎麼投遞組合鍵弄透呢?
vk_a = 65 // keys.a
wm_char = 258
wm_keyup = 257
wm_keydown = 256
vk_control = 17
keyeventf_keyup = 2
vk_shift = 16
vk_menu = 18 // alt
C 模擬鍵盤按鍵(ALT,CTRL
dllimport user32.dll static extern void keybd event byte bvk,byte bscan,uint dwflags,uint dwextrainfo dllimport user32.dll static extern byte mapvirtu...
模擬鍵盤按鍵
模擬全域性按鍵可以用keybd event,sendinput來完成,下面是模擬一次caps lock 按鍵時兩個api函式的實現方法 keybd event keybd event vk capital,0,keyeventf extendedkey 0,0 key down keybd even...
模擬鍵盤按鍵
textbox1.focus sendkeys.send v 模擬鍵盤按鍵ctrl v 貼上 鍵 backspace 或或break caps lock del或delete 或向下鍵 endenter 或 esc help home ins或insert 或向左鍵 num lock page do...