最近在游戏开发中遇到这样的需求:玩家需要不停的连击shift键。
大家都知道,在windows下默认连击shift键到5次会弹出粘滞键设置的对话框,如下图所示。
// activate or inactivate sticky keys hot key (pop up a set up dialog after continually pressed shift 5 times)
bool ActivateStickyHotkey(bool isActivate, bool* pIsPreviouslyActivate= NULL)
{
// fetch current sticky keys state
STICKYKEYS skf;
const DWORD datasize = sizeof(STICKYKEYS);
skf.cbSize = datasize;
if (!SystemParametersInfo(SPI_GETSTICKYKEYS, datasize, (LPVOID)&skf, 0))
{
return false;
}
const bool isPreviouslyActivate = (skf.dwFlags & SKF_HOTKEYACTIVE) != 0;
// store old sticky keys state
if (NULL!= pIsPreviouslyActivate)
{
*pIsPreviouslyActivate = isPreviouslyActivate;
}
// return true if no need to change state
if (isActivate==isPreviouslyActivate)
{
return true;
}
// change sticky keys state
skf.dwFlags = isActivate? (skf.dwFlags | SKF_HOTKEYACTIVE) : (skf.dwFlags & (~SKF_HOTKEYACTIVE));
const bool isSuccess = TRUE==SystemParametersInfo(SPI_SETSTICKYKEYS, datasize, (LPVOID)&skf, 0);
return isSuccess;
}
参考资料:
1,
2,


发布评论