getasynckeystate?VB GetAsyncKeyState函数问题,高手进
很多朋友对于getasynckeystate和VB GetAsyncKeyState函数问题,高手进不太懂,今天就由小编来为大家分享,希望可以帮助到大家,下面一起来看看吧!
VB GetAsyncKeyState 函数不准确,胡乱蹦字!怎么办
你不要简单地检测GetAsyncKeyState的返回值是否非零,因为如果在这个函数运行前指定的键已经被按过,则这个函数的返回值的位0为1;如果这个键目前处于按下状态,则位15为1。换句话说,GetAsyncKeyState这个函数可以返回键盘上某个键的两种实时状态:“曾经被按过”以及“正在被按下”!而这两种状态都会使GetAsyncKeyState的返回值非零,因此用If GetAsyncKeyState(vbKeyA) Then来判断A键是否被按下是不严密的,因为在你的程序启动之前,A键很可能已经被按过了,这种情况下你的程序一启动,GetAsyncKeyState(vbKeyA)就是为“真”的!
因此你要检测返回值的位15是否为1才能够精确判断这个键是不是正在被按着:
If(GetAsyncKeyState(vbKeyA) And&H8000)=&H8000 Then
另外,还有一个API函数GetKeyState是只检测按键的当前状态的(也就是说它不检测这个键是否“曾经被按过”),但仍然要检测位15是否为1(位0为1表示开关键打开,即CapsLock、NumLock、ScrollLock键):
If(GetKeyState(vbKeyA) And&H8000)=&H8000 Then
GetKeyState和GetAsyncKeyState的区别
VC中的GetKeyState和GetAsyncKeyState的区别
VC中添加组合快捷键时,经常会用到函数GetKeyState或函数GetAsyncKeyState,但是这两个函数有什么区别呢?
………………………………………………………………………………………………………………
使用::GetKeyState()返回一个short型的数,short型是16位有符号的数据类型,如果要查询的键被按下,返回值最高位被置1,则这个数表示负数,所以可以用<0或>0来判断。
0x8000是16进制数,用2进制表示为1000 0000 0000 0000,&是按位与
同样,如果键被按下,返回值最高位为1,则1xxx xxxx xxxx xxxx& 1000 0000 0000 0000得到的结果为1,否则为0,同样可以判断最高位的值。
需要说明的是,::GetKeyState()只能在键盘消息处理程序中使用,因为它只有在线程从消息队列中读取键盘消息时才会报告被查询键的状态,如果需要在键盘消息处理程序以外查询按键状态,则需要使用::GetAsyncKeyState来代替
………………………………………………………………………………………………………………
GetAsyncKeyState查询指定键的实时状态
使用后发现两者的区别(以组合键CTRL+A为例):
if(nChar==0x041)&&(GetKeyState(VK_CONTROL)&0x8000)//ctrl+A
{
//执行相应的操作
}
的效果是按下CTRL和A,实现指定的操作;(只有先按下CTRL然后按A或者同时按下二者,才能执行指定的操作)
而如果用
if(nChar==0x041)&& GetAsyncKeyState(VK_CONTROL))//ctrl+A
{
//执行相应的操作
}
得到的效果将是:按下CTRL,然后按下A,可以执行指定的操作,但是与上述的区别在于:
按下CTRL后隔了一段时间(可能是几秒或更长时间),然后再回过来按A键也会执行指定的操作(这样不是期望的效果)。
以上只是发现的一个区别,可能这两个函数还有其他的区别,但是还未遇到,因此在此也无法列出。
急!急!急!谁知道c++里的GetAsyncKeyState到底怎么用啊
下面是我的一点解释,“引号起来的内容注要意体会”,括号中的内容是我自己加的。例子是在测试程序具有键盘焦点的情况下进行的。
原型:
SHORT GetAsyncKeyState(int vKey);
参数vKey是某个按键的虚拟码
(很多虚拟码跟其ASCII码数值上相等,但不代表全部。)
MSDN片段:
The GetAsyncKeyState function determines whether a key is up or down at the time the function is called, and whether the key was pressed after a previous call to GetAsyncKeyState.
译文:
函数GetAsyncKeyState确定在调用它时某个按键处于弹起还是按下的,以及此按键是否在上一次调用GetAsyncKeyState之后(“又”)按下过(重复也算按下)。
MSDN片段:
If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. However, you should not rely on this last behavior; for more information, see the Remarks.
Windows NT/2000/XP: The return value is zero for the following cases:
The current desktop is not the active desktop
The foreground thread belongs to another process and the desktop does not allow the hook or the journal record.
Windows 95/98/Me: The return value is the global asynchronous key state for each virtual key. The system does not check which thread has the keyboard focus.
Windows 95/98/Me: Windows 95 does not support the left- and right-distinguishing constants. If you call GetAsyncKeyState with these constants, the return value is zero.
译文:
如果函数成功,则返回值指定此按键自上次上次调用GetAsyncKeyState后是否按下过,以及此按键当前是弹起的还是按下的。如果最高比特位被设置(就是被设为1),此按键就是按下的,如果最低位被设置,则此按键在上次调用GetAsyncKeyState之后(“又”)被按下过。但是你不应该依赖于之前的(按键)行为。更多的信息请看备注。
Windows NT/2000/XP:以下情况返回值为0
*当前桌面不是活动桌面(你有多个桌面的时候就会发生。)
*前台线程属于其他的进程并且桌面不允许次拦截(hook,名词时具有钩或类似物品的含义,动词使具有使用这些物品时的动作的含义,我采用一个根式和理解的词汇,但还不是很准)或日志记录。
Windows 95/98/Me:返回值是每个虚拟键全局异步按键状态。系统不会校对那个线程拥有键盘焦点。
Windows 95/98/Me: Windows 95不支持左右鉴别值。如果你用这些(包含左右鉴别值的)常量去调用GetAsyncKeyState时返回值为0。
举例:
假设下载我把A按住,调用GetAsyncKeyState('A')返回0x8001
在按键重复之前放掉A,调用GetAsyncKeyState('A')返回0x0000
又把A按住,又放掉,调用GetAsyncKeyState('A')返回0x0001
又把A按住,调用GetAsyncKeyState('A')
返回0x8001
在按键重复开始后放掉A,调用GetAsyncKeyState('A')返回0x0001
又调用GetAsyncKeyState('A')返回0x0000
VB GetAsyncKeyState函数问题,高手进
GetAsyncKeyState函数返回的是指定虚拟键瞬时的状态值,它有四种返回值:
0---键当前未处于按下状态,而且自上次调用GetAsyncKeyState后改键也未被按过;
1---键当前未处于按下状态,但在此之前(自上次调用GetAsyncKeyState后)键曾经被按过;
-32768(即16进制数&H8000)---键当前处于按下状态,但在此之前(自上次调用GetAsyncKeyState后)键未被按过;
-32767(即16进制数&H8001)---键当前处于按下状态,而且在此之前(自上次调用GetAsyncKeyState后)键也曾经被按过。
也就是说,你只要简单的判断GetAsyncKeyState的返回值是否为0,只要不为0就表示键被按下了。如果想精确点就判断一下返回值的二进制数位第0位或第15位是否为0。
OK,关于getasynckeystate和VB GetAsyncKeyState函数问题,高手进的内容到此结束了,希望对大家有所帮助。