Find the key currently pressed on the keyboard

zhaozj2021-02-08  264

I. Some of the issues of the problem hopes to identify the keys pressed on all keyboards to handle it correctly in the application. It is especially hope that the user presses the key Shift, Ctrl, Alt, Numlock or Capslock. For keys SHIFT, CTRL, and ALT, whether the user cares for whether the user is currently pressed; for the key Numlock and CapSlock, the programmer only cares about whether the user selects this button before the application acquires control. Second, Solution This is a very interesting question. How to determine which key on the keyboard is pressed on multiple-dimensional, multi-window system, so that the keyboard status is consistent with all applications? Since some keys may be pressed in additional windows, the pressing operation of the key cannot be simply captured. Since key Numlock may be pressed before the application is started, this button cannot be simply monitored. Therefore, it is necessary to complete this task to get a method of obtaining a keyboard state at any time. Fortunately, the Windows API provides a simple solution for this issue, that is, the API function getKeyboardState. Third, the implementation step Implement an example program in the following steps. Run this example, select the menu item keys Pressed from the menu keyboard, a dialog box that displays a series of keys, no text next to each button. Press a few keys and click on the button show, the status value on the right side of the list will change to reflect the current state of these keys. The specific steps of implementation examples are as follows: 1. In Visual C , create a new project file with AppWizard and name this item file for ld18.mak. 2. Enter AppStudio and create a new dialog. In the dialog box, add five text domains, the title is Control Key, Shift Key, Alt Key, Num Lock Key, and Caps Lock Key. 3. Align with the five text fields created, create another five text domain. The title is empty, the ID is: ID_Control_Key, ID_SHIFT_KEY, ID_ALT_KEY, ID_NUM_LOCK_KEY, and ID_CAPS_LOCK_KEY. 4. Change the title of the dialog as View Key State. 5. Add two buttons, the title is Show and Close, ID_show and IDOK, respectively. 6. Enter ClassWizard and select the button Add Class. Named dialog box is CKEYSHOWDLG. 7. In ClassWizard, select CKEYSHOWDLG from the drop-down list, select the button id_show from the list object, select the message command from the message list, click the button Add function, and the function is named onshow.

8. Enter the object CKeyShowDlg method OnShow following code: void CKeyShowDlg :: OnShow () {unsigned char kbuf [256]; GetKeyboardState (kbuf); if (kbuf [VK_CAPITAL] & 1) GetDlgItem (ID_CAPS_LOCK_KEY) -> SetWindowText ( " On "); else GetDlgItem (ID_CAPS_LOCK_KEY) -> SetWindowText (" Off "); if (kbuf [VK_SHIFT] & 128) GetDlgItem (ID_SHIFT_KEY) -> SetWindowText (" On "); else GetDlgItem (ID_SHIFT_KEY) -> SetWindowText (" Off "); if (kbuf [VK_CONTROL] & 128) GetDlgItem (ID_CTRL_KEY) -> SetWindowText (" On "); else GetDlgItem (ID_CTRL_KEY) -> SetWindowText (" Off "); if (kbuf [VK_MENU] & 128) GetDlgItem (ID_ALT_KEY) -> SetWindowText ( "On"); else GetDlgItem (ID_ALT_KEY) -> SetWindowText ( "Off"); if (kbuf [VK_NUMLOCK] & 1) GetDlgItem (ID_NUM_LOCK_KEY) -> SetWindowText ( "On"); else GetDlgItem (ID_NUM_LOCK_KEY) - > SetWindowText ("OFF");} 9. Enter AppStudio, add new menu Keyboard in menu idR_mainfame. In the menu Keybosrd, add the menu item keypsed, the ID is id_key_pressed. Enter ClassWizard to add a new function to object ID_key_pressed and message Command, name this function to ONKEYPRESSED, and add the following code in this function: void cmainframe :: onkeypressed () {ckeyshowdlg DLG; DLG.Domodal ();} 10. Document MAINFRM.CPP top Add Next: #include "keyshowdlg.h" 11. Compile and run this example. Usage The Windows API function getKeyboardState returns the status of each button on the keyboard. For keys that can be pressed and release, such as Shift, Ctrl, Tab, and all the character keys. "Status" or high setting (indicating that the key is pressed) or high clearance (the indication button is not pressed, "released). For switching selection, such as Num Lock or Caps Lock, the low setting indicates that this button is currently selected (open) state, and the low-level clearance indicates that this button is currently unselected (off) state. By checking the keyboard state at a predetermined time, you can get any keys and all keys pressed at any time.

转载请注明原文地址:https://www.9cbs.com/read-1421.html

New Post(0)