资源预览内容
第1页 / 共4页
第2页 / 共4页
第3页 / 共4页
第4页 / 共4页
亲,该文档总共4页全部预览完了,如果喜欢就下载吧!
资源描述
VBA中检测键盘控制键(Shift Control Alt)状态来源:水文工具集分类:代码,博客标签:VBAVBA编写的程序中,经常需要提供一些功能键或者快捷键或者是辅助键,比如Shift|Control|Alt,以方便用户更方便使用你的程序。VBA中并没有相关函数来完成检测键盘控制键状态的函数,因此,必须使用Windows API函数GetKeyState。GetKeyState函数会返回某个指定按键的状态(按下或者未按下)。水文工具集提供VBA编写的3个相关函数,可以方便你在代码中使用,用以完成相关功能。IsShiftKeyDown:检测Shift控制键状态IsControlKeyDown:检测Control控制键状态IsAltKeyDown:检测Alt控制键状态上面的函数返回True或者False,当指定按键按下时返回True,而未按下时返回False。函数的定义如下:01.PublicFunctionIsShiftKeyDown( _02.OptionalLeftOrRightKeyAsLong= LeftKeyOrRightKey _03.)AsBoolean04.05.PublicFunctionIsControlKeyDown( _06.OptionalLeftOrRightKeyAsLong= LeftKeyOrRightKey _07.)AsBoolean08.09.PublicFunctionIsAltKeyDown( _10.OptionalLeftOrRightKeyAsLong= LeftKeyOrRightKey _11.)AsBoolean上面的函数都有一个可选的参数来指定是检测只有左边或者只有右边或者两者其一或者同时按下控制键,默认是左边右边两者其一,具体的可选参数有下面的类型:1.PublicConstBothLeftAndRightKeys = 02. BOTH left and right together3.PublicConstLeftKey = 14. LEFT key only5.PublicConstRightKey = 26. RIGHT key only7.PublicConstLeftKeyOrRightKey = 38. EITHER left or right or BOTH当然如果加以修改,一样可以用来其它功能键。VBA中检测键盘控制键(Shift Control Alt)状态具体源代码如下:01.=02. VBA中检测键盘控制键(Shift Control Alt)状态03.04.http:/www.cnhup.com05.=06.OptionExplicit07.OptionCompare Text08.09.PrivateDeclareFunctionGetKeyStateLibuser32( _10.ByValnVirtKeyAsLong)AsInteger11.12.PrivateConstKEY_MASKAsInteger= &HFF80 decimal -12813.14.PrivateConstVK_LSHIFT = &HA015.PrivateConstVK_RSHIFT = &HA116.PrivateConstVK_LCONTROL = &HA217.PrivateConstVK_RCONTROL = &HA318.PrivateConstVK_LMENU = &HA419.PrivateConstVK_RMENU = &HA520.21.PrivateConstVK_LALT = VK_LMENU22.PrivateConstVK_RALT = VK_RMENU23.PrivateConstVK_LCTRL = VK_LCONTROL24.PrivateConstVK_RCTRL = VK_RCONTROL25.26.PublicConstBothLeftAndRightKeys = 027.PublicConstLeftKey = 128.PublicConstRightKey = 229.PublicConstLeftKeyOrRightKey = 330.31.PublicFunctionIsShiftKeyDown(OptionalLeftOrRightKeyAsLong= LeftKeyOrRightKey)AsBoolean32.DimResAsLong33.34.SelectCaseLeftOrRightKey35.CaseLeftKey36.Res = GetKeyState(VK_LSHIFT)AndKEY_MASK37.CaseRightKey38.Res = GetKeyState(VK_RSHIFT)AndKEY_MASK39.CaseBothLeftAndRightKeys40.Res = (GetKeyState(VK_LSHIFT)AndGetKeyState(VK_RSHIFT)AndKEY_MASK)41.CaseElse42.Res = GetKeyState(vbKeyShift)AndKEY_MASK43.EndSelect44.45.IsShiftKeyDown =CBool(Res)46.EndFunction47.48.PublicFunctionIsControlKeyDown(OptionalLeftOrRightKeyAsLong= LeftKeyOrRightKey)AsBoolean49.DimResAsLong50.51.SelectCaseLeftOrRightKey52.CaseLeftKey53.Res = GetKeyState(VK_LCTRL)AndKEY_MASK54.CaseRightKey55.Res = GetKeyState(VK_RCTRL)AndKEY_MASK56.CaseBothLeftAndRightKeys57.Res = (GetKeyState(VK_LCTRL)AndGetKeyState(VK_RCTRL)AndKEY_MASK)58.CaseElse59.Res = GetKeyState(vbKeyControl)AndKEY_MASK60.EndSelect61.62.IsControlKeyDown =CBool(Res)63.64.EndFunction65.66.PublicFunctionIsAltKeyDown(OptionalLeftOrRightKeyAsLong= LeftKeyOrRightKey)AsBoolean67.DimResAsLong68.69.SelectCaseLeftOrRightKey70.CaseLeftKey71.Res = GetKeyState(VK_LALT)AndKEY_MASK72.CaseRightKey73.Res = GetKeyState(VK_RALT)AndKEY_MASK74.CaseBothLeftAndRightKeys75.Res = (GetKeyState(VK_LALT)AndGetKeyState(VK_RALT)AndKEY_MASK)76.CaseElse77.Res = GetKeyState(vbKeyMenu)AndKEY_MASK78.EndSelect79.80.IsAltKeyDown =CBool(Res)81.82.EndFunction下面代码是调用了上面函数的一个测试,我们可以从VBA编辑器的调试窗口中看到相关控制键状态。01.SubTest()02.Application.OnTime Now + TimeSerial(0, 0, 2),ProcTest, ,True03.EndSub04.05.SubProcTest()06.Debug.PrintSHIFT KEY: , _07.LEFT: &CStr(IsShiftKeyDown(LeftKey), _08.RIGHT: &CStr(IsShiftKeyDown(RightKey), _09.EITHER: &CStr(IsShiftKeyDown(LeftKeyOrRightKey), _10.BOTH: &CStr(IsShiftKeyDown(BothLeftAndRightKeys)11.12.Debug.PrintALT KEY: , _13.LEFT: &CStr(IsAltKeyDown(LeftKey), _14.RIGHT: &CStr(IsAltKeyDown(RightKey), _15.EITHER: &CStr(IsAltKeyDown(LeftKeyOrRightKey), _16.BOTH: &CStr(IsAltKeyDown(BothLeftAndRightKeys)17.18.Debug.PrintCTRL KEY: , _19.LEFT: &CStr(IsControlKeyDown(LeftKey), _20.RIGHT: &CStr(IsControlKeyDown(RightKey), _21.EITHER: &CStr(IsControlKeyDown(LeftKeyOrRightKey), _22.BOTH: &CStr(IsControlKeyDown(BothLeftAndRightKeys)23.24.EndSub原创文章如转载,请注明:转载自水文工具集http:/www.cnhup.com
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号