鼠标键盘模拟

这篇博客介绍了如何在Windows环境下使用API函数keybd_event和mouse_event进行键盘和鼠标的模拟操作,包括发送按键、点击鼠标等。还提到了SendInput函数作为替代方法,并详细解释了这些函数的参数和使用场景。对于键盘模拟,讨论了大小写敏感性和CapsLock状态的影响。同时,文章提及了Windows NT/2000/XP之后版本推荐使用SendInput函数代替keybd_event和mouse_event。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

发送a字符到当前活动窗口,大小写和当前capslock有关 keybd_event(VK_CAPITAL, 0, 0 ,0); keybd_event(VK_CAPITAL, 0, KEYEVENTF_KEYUP ,0); keybd_event('A', 0, 0 ,0); keybd_event('A', 0, KEYEVENTF_KEYUP ,0); 具体的虚拟键的值见附表 mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0); mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0); POINT point; GetCursorPos(&point); SetCursorPos(500,500); Window2000/NT/XP已经不提倡用这个函数了,上面的方法只是为了让大家开阔一下思路,怎么替代呢,呵呵,看下面,所以上面的所有代码都可以用这个来完成 //2000下用这个代替 ,包含 "winable.h" 输入的Win+R的代码 INPUT input[4]; memset(input, 0, sizeof(input)); input[0].type = input[1].type = input[2].type = input[3].type = INPUT_KEYBOARD; input[0].ki.wVk = input[3].ki.wVk = VK_LWIN; input[1].ki.wVk = input[2].ki.wVk = 'R'; //接下来释放它,这一点很重要。 input[2].ki.dwFlags = input[3].ki.dwFlags = KEYEVENTF_KEYUP; input[0].ki.time = input[1].ki.time = input[2].ki.time = input[3].ki.time = GetTickCount(); SendInput(4, input, sizeof(INPUT)); 键盘输入模拟代码 SetCursorPos(500,500); INPUT input[2]; memset(input, 0, sizeof(input)); input[0].type = input[1].type = INPUT_MOUSE; input[0].mi.dwFlags = MOUSEEVENTF_RIGHTDOWN; input[1].mi.dwFlags = MOUSEEVENTF_RIGHTUP; input[0].ki.time = input[1].ki.time = GetTickCount(); SendInput(2, input, sizeof(INPUT)); The SendInput function synthesizes keystrokes, mouse motions, and button clicks. Syntax UINT SendInput( UINT nInputs, LPINPUT pInputs, int cbSize ); Parameters nInputs [in] Number of structures in the pInputs array. pInputs [in] Pointer to an array of INPUT structures. Each structure represents an event to be inserted into the keyboard or mouse input stream. cbSize [in] Specifies the size, in bytes, of an INPUT structure. If cbSize is not the size of an INPUT structure, the function fails. Return Value The function returns the number of events that it successfully inserted into the keyboard or mouse input stream. If the function returns zero, the input was already blocked by another thread. To get extended error information, call GetLastError. Microsoft Windows Vista. This function fails when it is blocked by User Interface Privilege Isolation (UIPI). Note that neither GetLastError nor the return value will indicate the failure was caused by UIPI blocking. The INPUT structure is used by SendInput to store information for synthesizing input events such as keystrokes, mouse movement, and mouse clicks. Syntax typedef struct tagINPUT { DWORD type; union {MOUSEINPUT mi; KEYBDINPUT ki; HARDWAREINPUT hi; }; }INPUT, *PINPUT; Members type Specifies the type of the input event. This member can be one of the following values. INPUT_MOUSE The event is a mouse event. Use the mi structure of the union. INPUT_KEYBOARD The event is a keyboard event. Use the ki structure of the union. INPUT_HARDWARE Windows 95/98/Me: The event is from input hardware other than a keyboard or mouse. Use the hi structure of the union. mi A MOUSEINPUT structure that contains information about a simulated mouse event. ki A KEYBDINPUT structure that contains information about a simulated keyboard event. hi Windows 95/98/Me: A HARDWAREINPUT structure that contains information about a simulated event from input hardware other than a keyboard or mouse. Remarks This structure contains information identical to that used in the parameter list of the keybd_event or mouse_event function. Windows 2000/XP: INPUT_KEYBOARD supports nonkeyboard input methods, such as handwriting recognition or voice recognition, as if it were text input by using the KEYEVENTF_UNICODE flag. For more information, see the remarks section of KEYBDINPUT. The keybd_event function synthesizes a keystroke. The system can use such a synthesized keystroke to generate a WM_KEYUP or WM_KEYDOWN message. The keyboard driver's interrupt handler calls the keybd_event function. Windows NT/2000/XP/Vista:This function has been superseded. Use SendInput instead. Syntax VOID keybd_event( BYTE bVk, BYTE bScan, DWORD dwFlags, PTR dwExtraInfo ); Parameters bVk [in] Specifies a virtual-key code. The code must be a value in the range 1 to 254. For a complete list, see Virtual-Key Codes. bScan Specifies a hardware scan code for the key. dwFlags [in] Specifies various aspects of function operation. This parameter can be one or more of the following values. KEYEVENTF_EXTENDEDKEY If specified, the scan code was preceded by a prefix byte having the value 0xE0 (224). KEYEVENTF_KEYUP If specified, the key is being released. If not specified, the key is being depressed. dwExtraInfo [in] Specifies an additional value associated with the key stroke. Return Value This function has no return value. The mouse_event function synthesizes mouse motion and button clicks. Windows NT/2000/XP: This function has been superseded. Use SendInput instead. Syntax VOID mouse_event( DWORD dwFlags, DWORD dx, DWORD dy, DWORD dwData, ULONG_PTR dwExtraInfo ); Parameters dwFlags [in] Specifies various aspects of mouse motion and button clicking. This parameter can be certain combinations of the following values. The values that specify mouse button status are set to indicate changes in status, not ongoing conditions. For example, if the left mouse button is pressed and held down, MOUSEEVENTF_LEFTDOWN is set when the left button is first pressed, but not for subsequent motions. Similarly, MOUSEEVENTF_LEFTUP is set only when the button is first released. You cannot specify both MOUSEEVENTF_WHEEL and either MOUSEEVENTF_XDOWN or MOUSEEVENTF_XUP simultaneously in the dwFlags parameter, because they both require use of the dwData field. MOUSEEVENTF_ABSOLUTE Specifies that the dx and dy parameters contain normalized absolute coordinates. If not set, those parameters contain relative data: the change in position since the last reported position. This flag can be set, or not set, regardless of what kind of mouse or mouse-like device, if any, is connected to the system. For further information about relative mouse motion, see the following Remarks section. MOUSEEVENTF_MOVE Specifies that movement occurred. MOUSEEVENTF_LEFTDOWN Specifies that the left button is down. MOUSEEVENTF_LEFTUP Specifies that the left button is up. MOUSEEVENTF_RIGHTDOWN Specifies that the right button is down. MOUSEEVENTF_RIGHTUP Specifies that the right button is up. MOUSEEVENTF_MIDDLEDOWN Specifies that the middle button is down. MOUSEEVENTF_MIDDLEUP Specifies that the middle button is up. MOUSEEVENTF_WHEEL Windows NT/2000/XP: Specifies that the wheel has been moved, if the mouse has a wheel. The amount of movement is specified in dwData MOUSEEVENTF_XDOWN Windows 2000/XP: Specifies that an X button was pressed. MOUSEEVENTF_XUP Windows 2000/XP: Specifies that an X button was released. dx [in] Specifies the mouse's absolute position along the x-axis or its amount of motion since the last mouse event was generated, depending on the setting of MOUSEEVENTF_ABSOLUTE. Absolute data is specified as the mouse's actual x-coordinate; relative data is specified as the number of mickeys moved. A mickey is the amount that a mouse has to move for it to report that it has moved. dy [in] Specifies the mouse's absolute position along the y-axis or its amount of motion since the last mouse event was generated, depending on the setting of MOUSEEVENTF_ABSOLUTE. Absolute data is specified as the mouse's actual y-coordinate; relative data is specified as the number of mickeys moved. dwData [in] If dwFlags contains MOUSEEVENTF_WHEEL, then dwData specifies the amount of wheel movement. A positive value indicates that the wheel was rotated forward, away from the user; a negative value indicates that the wheel was rotated backward, toward the user. One wheel click is defined as WHEEL_DELTA, which is 120. If dwFlags contains MOUSEEVENTF_HWHEEL, then dwData specifies the amount of wheel movement. A positive value indicates that the wheel was rotated to the right; a negative value indicates that the wheel was rotated to the left. One wheel click is defined as WHEEL_DELTA, which is 120. Windows 2000/XP: If dwFlags contains MOUSEEVENTF_XDOWN or MOUSEEVENTF_XUP, then dwData specifies which X buttons were pressed or released. This value may be any combination of the following flags. If dwFlags is not MOUSEEVENTF_WHEEL, MOUSEEVENTF_XDOWN, or MOUSEEVENTF_XUP, then dwData should be zero. XBUTTON1 Set if the first X button was pressed or released. XBUTTON2 Set if the second X button was pressed or released. dwExtraInfo [in] Specifies an additional value associated with the mouse event. An application calls GetMessageExtraInfo to obtain this extra information. Return Value This function has no return value. Virtual-Key Codes -------------------------------------------------------------------------------- The following table shows the symbolic constant names, hexadecimal values, and mouse or keyboard equivalents for the virtual-key codes used by the system. The codes are listed in numeric order. Constants VK_LBUTTON (01) Left mouse button VK_RBUTTON (02) Right mouse button VK_CANCEL (03) Control-break processing VK_MBUTTON (04) Middle mouse button (three-button mouse) VK_XBUTTON1 (05) Windows 2000/XP: X1 mouse button VK_XBUTTON2 (06) Windows 2000/XP: X2 mouse button - (07) Undefined VK_BACK (08) BACKSPACE key VK_TAB (09) TAB key - (0A-0B) Reserved VK_CLEAR (0C) CLEAR key VK_RETURN (0D) ENTER key - (0E-0F) Undefined VK_SHIFT (10) SHIFT key VK_CONTROL (11) CTRL key VK_MENU (12) ALT key VK_PAUSE (13) PAUSE key VK_CAPITAL (14) CAPS LOCK key VK_KANA (15) Input Method Editor (IME) Kana mode VK_HANGUEL (15) IME Hanguel mode (maintained for compatibility; use VK_HANGUL) VK_HANGUL (15) IME Hangul mode - (16) Undefined VK_JUNJA (17) IME Junja mode VK_FINAL (18) IME final mode VK_HANJA (19) IME Hanja mode VK_KANJI (19) IME Kanji mode - (1A) Undefined VK_ESCAPE (1B) ESC key VK_CONVERT (1C) IME convert VK_NONCONVERT (1D) IME nonconvert VK_ACCEPT (1E) IME accept VK_MODECHANGE (1F) IME mode change request VK_SPACE (20) SPACEBAR VK_PRIOR (21) PAGE UP key VK_NEXT (22) PAGE DOWN key VK_END (23) END key VK_HOME (24) HOME key VK_LEFT (25) LEFT ARROW key VK_UP (26) UP ARROW key VK_RIGHT (27) RIGHT ARROW key VK_DOWN (28) DOWN ARROW key VK_SELECT (29) SELECT key VK_PRINT (2A) PRINT key VK_EXECUTE (2B) EXECUTE key VK_SNAPSHOT (2C) PRINT SCREEN key VK_INSERT (2D) INS key VK_DELETE (2E) DEL key VK_HELP (2F) HELP key (30) 0 key (31) 1 key (32) 2 key (33) 3 key (34) 4 key (35) 5 key (36) 6 key (37) 7 key (38) 8 key (39) 9 key - (3A-40) Undefined (41) A key (42) B key (43) C key (44) D key (45) E key (46) F key (47) G key (48) H key (49) I key (4A) J key (4B) K key (4C) L key (4D) M key (4E) N key (4F) O key (50) P key (51) Q key (52) R key (53) S key (54) T key (55) U key (56) V key (57) W key (58) X key (59) Y key (5A) Z key VK_LWIN (5B) Left Windows key (Microsoft Natural keyboard) VK_RWIN (5C) Right Windows key (Natural keyboard) VK_APPS (5D) Applications key (Natural keyboard) - (5E) Reserved VK_SLEEP (5F) Computer Sleep key VK_NUMPAD0 (60) Numeric keypad 0 key VK_NUMPAD1 (61) Numeric keypad 1 key VK_NUMPAD2 (62) Numeric keypad 2 key VK_NUMPAD3 (63) Numeric keypad 3 key VK_NUMPAD4 (64) Numeric keypad 4 key VK_NUMPAD5 (65) Numeric keypad 5 key VK_NUMPAD6 (66) Numeric keypad 6 key VK_NUMPAD7 (67) Numeric keypad 7 key VK_NUMPAD8 (68) Numeric keypad 8 key VK_NUMPAD9 (69) Numeric keypad 9 key VK_MULTIPLY (6A) Multiply key VK_ADD (6B) Add key VK_SEPARATOR (6C) Separator key VK_SUBTRACT (6D) Subtract key VK_DECIMAL (6E) Decimal key VK_DIVIDE (6F) Divide key VK_F1 (70) F1 key VK_F2 (71) F2 key VK_F3 (72) F3 key VK_F4 (73) F4 key VK_F5 (74) F5 key VK_F6 (75) F6 key VK_F7 (76) F7 key VK_F8 (77) F8 key VK_F9 (78) F9 key VK_F10 (79) F10 key VK_F11 (7A) F11 key VK_F12 (7B) F12 key VK_F13 (7C) F13 key VK_F14 (7D) F14 key VK_F15 (7E) F15 key VK_F16 (7F) F16 key VK_F17 (80H) F17 key VK_F18 (81H) F18 key VK_F19 (82H) F19 key VK_F20 (83H) F20 key VK_F21 (84H) F21 key VK_F22 (85H) F22 key VK_F23 (86H) F23 key VK_F24 (87H) F24 key - (88-8F) Unassigned VK_NUMLOCK (90) NUM LOCK key VK_SCROLL (91) SCROLL LOCK key (92-96) OEM specific - (97-9F) Unassigned VK_LSHIFT (A0) Left SHIFT key VK_RSHIFT (A1) Right SHIFT key VK_LCONTROL (A2) Left CONTROL key VK_RCONTROL (A3) Right CONTROL key VK_LMENU (A4) Left MENU key VK_RMENU (A5) Right MENU key VK_BROWSER_BACK (A6) Windows 2000/XP: Browser Back key VK_BROWSER_FORWARD (A7) Windows 2000/XP: Browser Forward key VK_BROWSER_REFRESH (A8) Windows 2000/XP: Browser Refresh key VK_BROWSER_STOP (A9) Windows 2000/XP: Browser Stop key VK_BROWSER_SEARCH (AA) Windows 2000/XP: Browser Search key VK_BROWSER_FAVORITES (AB) Windows 2000/XP: Browser Favorites key VK_BROWSER_HOME (AC) Windows 2000/XP: Browser Start and Home key VK_VOLUME_MUTE (AD) Windows 2000/XP: Volume Mute key VK_VOLUME_DOWN (AE) Windows 2000/XP: Volume Down key VK_VOLUME_UP (AF) Windows 2000/XP: Volume Up key VK_MEDIA_NEXT_TRACK (B0) Windows 2000/XP: Next Track key VK_MEDIA_PREV_TRACK (B1) Windows 2000/XP: Previous Track key VK_MEDIA_STOP (B2) Windows 2000/XP: Stop Media key VK_MEDIA_PLAY_PAUSE (B3) Windows 2000/XP: Play/Pause Media key VK_LAUNCH_MAIL (B4) Windows 2000/XP: Start Mail key VK_LAUNCH_MEDIA_SELECT (B5) Windows 2000/XP: Select Media key VK_LAUNCH_APP1 (B6) Windows 2000/XP: Start Application 1 key VK_LAUNCH_APP2 (B7) Windows 2000/XP: Start Application 2 key - (B8-B9) Reserved VK_OEM_1 (BA) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the ';:' key VK_OEM_PLUS (BB) Windows 2000/XP: For any country/region, the '+' key VK_OEM_COMMA (BC) Windows 2000/XP: For any country/region, the ',' key VK_OEM_MINUS (BD) Windows 2000/XP: For any country/region, the '-' key VK_OEM_PERIOD (BE) Windows 2000/XP: For any country/region, the '.' key VK_OEM_2 (BF) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the '/?' key VK_OEM_3 (C0) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the '`~' key - (C1-D7) Reserved - (D8-DA) Unassigned VK_OEM_4 (DB) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the '[{' key VK_OEM_5 (DC) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the '/|' key VK_OEM_6 (DD) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the ']}' key VK_OEM_7 (DE) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the 'single-quote/double-quote' key VK_OEM_8 (DF) Used for miscellaneous characters; it can vary by keyboard. - (E0) Reserved (E1) OEM specific VK_OEM_102 (E2) Windows 2000/XP: Either the angle bracket key or the backslash key on the RT 102-key keyboard (E3-E4) OEM specific VK_PROCESSKEY (E5) Windows 95/98/Me, Windows NT 4.0, Windows 2000/XP: IME PROCESS key (E6) OEM specific VK_PACKET (E7) Windows 2000/XP: Used to pass Unicode characters as if they were keystrokes. The VK_PACKET key is the low word of a 32-bit Virtual Key value used for non-keyboard input methods. For more information, see Remark in KEYBDINPUT, SendInput, WM_KEYDOWN, and WM_KEYUP - (E8) Unassigned (E9-F5) OEM specific VK_ATTN (F6) Attn key VK_CRSEL (F7) CrSel key VK_EXSEL (F8) ExSel key VK_EREOF (F9) Erase EOF key VK_PLAY (FA) Play key VK_ZOOM (FB) Zoom key VK_NONAME (FC) Reserved VK_PA1 (FD) PA1 key VK_OEM_CLEAR (FE) Clear key
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值