如果是放在 窗体模块 中:
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
如果是放在 模块 中:
Public Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
调用方式:
在任意过程或者函数中执行判断:
If GetKeyState(vbKeyShift) <0 then msgbox "按下了shift键" else msgbox "没有按下shift键" end If
同理可以延伸出检测ctrl与Alt是否被按住的功能:
' 获取 Ctrl 键的状态 If GetKeyState(vbKeyControl)< 0 Then MsgBox "Ctrl 键被按下" Else MsgBox "Ctrl 键未被按下" End If ' 获取 Alt 键的状态 If GetKeyState(vbKeyMenu)< 0 Then MsgBox "Alt 键被按下" Else MsgBox "Alt 键未被按下" End If
标签:End,Lib,Ctrl,shift,ACCESS,API,Alt,MsgBox,GetKeyState From: https://www.cnblogs.com/yoooos/p/17824219.html