您可以使用 SetTimer 每秒生成 20 次左右的 WM_TIMER 事件
SetTimer( NULL, kMyTimer, 50, MyTimerCallback );
然后实现如下函数。
void CALLBACK MyTimerCallback( HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime )
{
static short lastLeftAltPress = 0;
short thisLeftAltPress = GetAsyncKeyState( VK_LMENU );
if ( thisLeftAltPress != 0 && lastLeftAltPress == 0 )
{
CallAltHandlingCode();
}
thisLeftAltPress = lastLestAltPress;
// Handling code for other keys goes here.
}
这将每 50 毫秒轮询一次键盘,以查明是否刚刚按下左 alt 键,然后调用您的处理代码。如果你想在处理程序释放时触发它,那么你可以使用下面的 if 语句
if ( thisLeftAltPress == 0 && lastLeftAltPress != 0 )
或者如果你只是想看看它是否已经关闭,那么你就这样做
if ( thisLeftAltPress != 0 )
GetAsyncKeyState 的文档确实声明您可以检查是否设置了最低位以查看键是否刚刚被按下,但它也指出这可能会在多线程环境中以意想不到的方式失败。上述方案应该始终有效。