GetKeyState GetAsyncKeyState

71 篇文章 0 订阅



转自百度百科
GetKeyState
函数功能:该函数检取指定虚拟键的状态。该状态指定此键是UP状态,DOWN状态,还是被触发的(开关每次按下此键时进行切换)。
函数原型:SHORT GetKeyState(int nVirtKey);
函数:
nVrtKey:定义一虚拟键。若要求的虚拟键是字母或数字(A~Z,a~z或0~9),nVirtKey必须被置为相应 字符的ASCII码值,对于其他的键,nVirtKey必须是一 虚拟键码。若使用非英语 键盘布局,则取值在ASCIIa~z和0~9的虚拟键被用于定义绝大多数的 字符键。例如,对于德语 键盘格式,值为ASCII0(OX4F)的虚拟键指的是"0"键,而VK_OEM_1指"带变音的0键"

编辑本段返回值

例子:
::GetKeyState(VK_SHIFT) > 0 没按下
::GetKeyState(VK_SHIFT) < 0被按下
返回值给出了给定虚拟键的状态,状态如下:
若高序位为1,则键处于DOWN状态,否则为UP状态。
若低序位为1,则键被触发。例如CAPS LOCK键,被找开时将被触发。若低序位置为0,则键被关闭,且不被触发。触发键在 键盘上的指示灯,当键被触发时即亮,键不被触发时即灭。
备注:当给定线程从它的 消息队列中读键消息时,该函数返回的键状态发生改变。该状态并不反映与硬件相关的中断级的状态。使用GetAsyncKeyState可获取这一信息。
应用程序可以使用GetKeyState来响应一个由 键盘输入产生的消息。此时该程序获得的是在输入消息生成时该键位的状态。
欲检取所有虚拟键状态信息,可以使用GetKeyboardState函数。
应用程序可以使用 虚拟键码常数VK_SHIFT,VK_CONTROL和VK_MENU作为nVirtKey参数的值。它给出shift,ctrl或alt键的值而不区分左右键,应用程序也可以使用如下的 虚拟键码常数作nVirtKey的值来区分前述键的左键、右键的情形。
VK_LSHIFT,VK_RSHIFT;VK_LCONTROL,VK_RCONTROL;VK_LMENU,VK_RMENU。
仅当应用程序调用GetKeyboardSlate,SetKeyboardState,GetAsyncKeystate;GetKeyState和MapVirtualKey函数时,才可用这些区分左右键的常数。
Windows CE:GetKeyState函数仅能用于检查如下虚拟键的DOWN状态。
VK_LSHIFT,VKRSHIFT,VK_LCONTROL;VK_RCONTROL;VK_LMENU,VK_RMENU。
GetKeyState函数只能用于检查VK_CAPITAL虚拟键的触发状态。
速查:Windows NT:3.1及以上版本;Windows:95及以上版本;Windows CE:1.I及以上版本;头文件:
winuser.h;库文件:user32.lib。
GetKeyState Function
Declare Function GetKeyState Lib "user32.dll" (ByVal nVirtKey As Long) As Integer
GetKeyState returns the current status of one of the keys on the keyboard. This status contains two pieces of
information: the key's toggle state and the key's pressed state. The information is put into the return value. The
toggle state is analogous to the toggle nature of the Caps Lock, Num Lock, and Scroll Lock keys, but Windows
records toggle information about every key. The toggle information is stored in bit &H80 of the return value.
The pressed state is true if the key is currently being depressed. The pressed information is stored in bit &H01
of the return value. See the example for more information on how to use the return value.
nVirtKey
The virtual key code of the key to read the status of.
Example:
' Read the status of the Enter key
x = GetKeyState(&H0D) ' Enter's virtual key code is &H0D
If (x And &H80) = &H80 Then ' check to see if it is toggled
Debug.Print "Enter is currently toggled."
End If
If (x And &H01) = &H01 Then ' check to see if it is depressed
Debug.Print "Enter is currently depressed."
End If

GetAsyncKeyState
功能:确定用户当前是否按下了键盘上的一个键
原型:SHORT GetAsyncKeyState(int vKey);
参数:nVirtKey指出要检查键的虚键代码。结果的高位指出该键当前是否被按下(是为1,否为0)。
常用键的VK值:
VK_SHIFT Shift键
VK_LSHIFT 左Shift键
VK_RSHIFT 右Shift键
VK_CONTROL Ctrl键
VK_LCONTROL 左Ctrl键
VK_RCONTROL 右Ctril键
VK_MENU Alt键
VK_LMENU 左Alt键
VK_RMENU 右Alt键
VK_LBUTTON 鼠标左键
VK_RBUTTON 鼠标右键
另一个函数GetKeyState与GetAsyncKeyState函数不同。GetAsyncKeyState在按下某键的同时调用,判断正在按下某键。
GetKeyState则在按过某键之后再调用,它返回最近的键盘消息从 线程的队列中移出时的键盘状态,判断刚按过了某键。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值