keybd_event函数详解(请别忽视了scan codes!)

来源:互联网 发布:麻将胡牌算法 编辑:程序博客网 时间:2024/05/17 06:54

Introduction

Simulation of a keyboard input is a well known concept for those who are all familiar with Visual Basic. SendKeys() in Visual Basic does all the things, if you want to do anything without keyboard. But what is in SendKeys() function? What does it do? Can we do such a thing with Visual C++? This article is the answer. I think this will be useful for beginners who are all just trying to do something different with VC++. Let us get into the steps�

Function Syntax

Collapse Copy Code
void keybd_event(BYTE bVirtualKey, BYTE bScanCode,                          DWORD dwFlags, DWORD dwExtraInfo);
  • bVirtualKey //Virtual Keycode of keys. E.g., VK_RETURN, VK_TAB�
  • bScanCode //Scan Code value of keys. E.g., 0xb8 for �Left Alt� key.
  • dwFlags //Flag that is set for key state. E.g., KEYEVENTF_KEYUP.
  • dwExtraInfo //32-bit extra information about keystroke.

Function Details:

  • bVirtualKey

    Virtual keycode that has to be send as key input. The following are the available predefined virtual key codes:

    VK_NUMPAD70x67VK_BACK0x08VK_NUMPAD80x68VK_TAB0x09VK_NUMPAD90x69VK_RETURN0x0DVK_MULTIPLY0x6AVK_SHIFT0x10VK_ADD0x6BVK_CONTROL0x11VK_SEPARATOR0x6CVK_MENU0x12VK_SUBTRACT0x6DVK_PAUSE0x13VK_DECIMAL0x6EVK_CAPITAL0x14VK_DIVIDE0x6FVK_ESCAPE0x1BVK_F10x70VK_SPACE0x20VK_F20x71VK_END0x23VK_F30x72VK_HOME0x24VK_F40x73VK_LEFT0x25VK_F50x74VK_UP0x26VK_F60x75VK_RIGHT0x27VK_F70x76VK_DOWN0x28VK_F80x77VK_PRINT0x2AVK_F90x78VK_SNAPSHOT0x2CVK_F100x79VK_INSERT0x2DVK_F110x7AVK_DELETE0x2EVK_F120x7BVK_LWIN0x5BVK_NUMLOCK0x90VK_RWIN0x5CVK_SCROLL0x91VK_NUMPAD00x60VK_LSHIFT0xA0VK_NUMPAD10x61VK_RSHIFT0xA1VK_NUMPAD20x62VK_LCONTROL0xA2VK_NUMPAD30x63VK_RCONTROL0xA3VK_NUMPAD40x64VK_LMENU0xA4VK_NUMPAD50x65VK_RMENU0xA5VK_NUMPAD60x66

    Character key can be converted into virtual key using VkKeyScan(TCHAR ch) function.

  • bScanCode

    Scan code is the hardware key code for the key (make and break codes). The following are the available scan codes (break code will be used in this parameter).

  • dwFlags

    A set of flag bits that specify various aspects of function operation. An application can use any combination of the following predefined constant values to set the flags.

    ValueMeaningKEYEVENTF_EXTENDEDKEYIf specified, the scan code was preceded by a prefix byte having the value 0xE0 (224).KEYEVENTF_KEYUPIf specified, the key is being released. If not specified, the key is being depressed.
  • dwExtraInfo

    32-bit extra information along with the keyboard input.

Example Code

Collapse Copy Code
// Simulating a Alt+Tab keystrokekeybd_event(VK_MENU,0xb8,0 , 0); //Alt Presskeybd_event(VK_TAB,0x8f,0 , 0); // Tab Presskeybd_event(VK_TAB,0x8f, KEYEVENTF_KEYUP,0); // Tab Releasekeybd_event(VK_MENU,0xb8,KEYEVENTF_KEYUP,0); // Alt Release// Simulating a Ctrl+A keystrokekeybd_event(VK_CONTROL,0x9d,0 , 0); // Ctrl Presskeybd_event(VkKeyScan(�A�),0x9e,0 , 0); // �A� Presskeybd_event(VkKeyScan(�A�),0x9e, KEYEVENTF_KEYUP,0); // �A� Releasekeybd_event(VK_CONTROL,0x9d,KEYEVENTF_KEYUP,0); // Ctrl Release

Conclusion

This article may not be that much detailed. None of the articles can satisfy one's expectations. But, each article should be a seed for your technical growth. Thus, I believe that this would be a seed. Thank you all.

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

 

原创粉丝点击