分享

C 语言实现模拟键盘输入

 昵称14067080 2013-09-30

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.

Note  This function has been superseded. Use SendInput instead.

Syntax

VOID WINAPI keybd_event(
  _In_  BYTE bVk,
  _In_  BYTE bScan,
  _In_  DWORD dwFlags,
  _In_  ULONG_PTR dwExtraInfo
);

Parameters

bVk [in]

Type: BYTE

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 [in]

Type: BYTE

A hardware scan code for the key.

dwFlags [in]

Type: DWORD

Controls various aspects of function operation. This parameter can be one or more of the following values.

ValueMeaning
KEYEVENTF_EXTENDEDKEY
0x0001

If specified, the scan code was preceded by a prefix byte having the value 0xE0 (224).

KEYEVENTF_KEYUP
0x0002

If specified, the key is being released. If not specified, the key is being depressed.

 

dwExtraInfo [in]

Type: ULONG_PTR

An additional value associated with the key stroke.

Return value

This function does not return a value.

Remarks

An application can simulate a press of the PRINTSCRN key in order to obtain a screen snapshot and save it to the clipboard. To do this, call keybd_event with the bVk parameter set to VK_SNAPSHOT.

Examples

The following sample program toggles the NUM LOCK light by using keybd_event with a virtual key of VK_NUMLOCK. It takes a Boolean value that indicates whether the light should be turned off (FALSE) or on (TRUE). The same technique can be used for the CAPS LOCK key (VK_CAPITAL) and the SCROLL LOCK key (VK_SCROLL).

   #include <windows.h>

   void SetNumLock( BOOL bState )
   {
      BYTE keyState[256];

      GetKeyboardState((LPBYTE)&keyState);
      if( (bState && !(keyState[VK_NUMLOCK] & 1)) ||
          (!bState && (keyState[VK_NUMLOCK] & 1)) )
      {
      // Simulate a key press
         keybd_event( VK_NUMLOCK,
                      0x45,
                      KEYEVENTF_EXTENDEDKEY | 0,
                      0 );

      // Simulate a key release
         keybd_event( VK_NUMLOCK,
                      0x45,
                      KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
                      0);
      }
   }

   void main()
   {
      SetNumLock( TRUE );
   }


Requirements

Minimum supported client

Windows 2000 Professional [desktop apps only]

Minimum supported server

Windows 2000 Server [desktop apps only]

Header

Winuser.h (include Windows.h)

Library

User32.lib

DLL

User32.dll


Scan Codes

Key 	Scan Code
~ ` 29
! 1 2
@ 2 3
# 3 4
$ 4 5
% 5 6
^ 6 7
& 7 8
* 8 9
( 9 0A
) 0 0B
_ - 0C
+ = 0D
Backspace 0E
Tab 0F
Q 10
W 11
E 12
R 13
T 14
Y 15
U 16
I 17
O 18
P 19
{ [ 1A
} ] 1B
| \ 2B
Caps Lock 3A
A 1E
S 1F
D 20
F 21
G 22
H 23
J 24
K 25
L 26
: ; 27
“ ' 28
Enter 1C
L SHIFT 2A
Z 2C
X 2D
C 2E
V 2F
B 30
N 31
M 32
< , 33
> . 34
/ 35
R SHIFT 36
L CTRL 1D
L ALT 38
Space Bar 39
R ALT E0 38
R CTRL E0 1D
Insert E0 52
Delete E0 53
L Arrow E0 4B
Home E0 47
End E0 4F
Up Arrow E0 48
Dn Arrow E0 50
Page Up E0 49
Page Down E0 51
R Arrow E0 4D
Num Lock 45
Numeric 7 47
Numeric 4 4B
Numeric 1 4F
Numeric / E0 35
Numeric 8 48
Numeric 5 4C
Numeric 2 50
Numeric 0 52
Numeric * 37
Numeric 9 49
Numeric 6 4D
Numeric 3 51
Numeric . 53
Numeric - 4A
Numeric + 4E
Numeric Enter E0 1C
Esc 1
F1 3B
F2 3C
F3 3D
F4 3E
F5 3F
F6 40
F7 41
F8 42
F9 43
F10 44
F11 57
F12 58
Print Screen ??
Scroll Lock 46
Pause ??
Left Win E0 5B
Right Win E0 5C
Application E0 5D
ACPI Power E0 5E
ACPI Sleep E0 5F
ACPI Wake E0 63

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多