C++通过GetKeyState获取键盘鼠标的按键状态

本文介绍了一个简单的C++程序,用于获取Windows环境下键盘特定按键的状态,包括大写锁定键和数字锁定键。通过调用GetKeyState函数,可以检测到这些按键是否被按下。

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

先上代码:

#include <iostream>
#include <Windows.h>
int main()
{
    SHORT my_CAPITAL= GetKeyState(VK_CAPITAL);
    SHORT my_NUMLOCK= GetKeyState(VK_NUMLOCK);
}

大写锁定返回1,未锁定返回0,如果在函数执行时,还在按着,那就是-127,如下图:

 官网用法:

Virtual-Key Codes (Winuser.h) - Win32 apps | Microsoft Learn

函数定义: 

SHORT GetKeyState(
  [in] int nVirtKey
);

参数:

PaParameters

[in] nVirtKey

Type: int

A virtual key. If the desired virtual key is a letter or digit (A through Z, a through z, or 0 through 9), nVirtKey must be set to the ASCII value of that character. For other keys, it must be a virtual-key code.

If a non-English keyboard layout is used, virtual keys with values in the range ASCII A through Z and 0 through 9 are used to specify most of the character keys. For example, for the German keyboard layout, the virtual key of value ASCII O (0x4F) refers to the "o" key, whereas VK_OEM_1 refers to the "o with umlaut" key.

是一个ASCII 值,具体的对应定义为:

GetKeyState function (winuser.h) - Win32 apps | Microsoft Learn

防止链接失效,以下是内容: 

Constant Value Description
VK_LBUTTON 0x01 Left mouse button
VK_RBUTTON 0x02 Right mouse button
VK_CANCEL 0x03 Control-break processing
VK_MBUTTON 0x04 Middle mouse button (three-button mouse)
VK_XBUTTON1 0x05 X1 mouse button
VK_XBUTTON2 0x06 X2 mouse button
- 0x07 Undefined
VK_BACK 0x08 BACKSPACE key
VK_TAB 0x09 TAB key
- 0x0A-0B Reserved
VK_CLEAR 0x0C CLEAR key
VK_RETURN</
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值