检查键盘按键或鼠标/游戏杆按钮是否按下或放开. 也可以获取游戏杆的状态.
GetKeyState, OutputVar, KeyName [, Mode]
KeyIsDown := GetKeyState("KeyName" [, "Mode"])
OutputVar | 用于保存获取的按键状态的变量名称, 它的值当按下时是D, 弹起时是U (不过 GetKeyState() 函数当按下时返回 true (1), 弹起时返回 false (0)). 如果按键的状态无法识别, 此变量将为空. 关于游戏杆的下面几点说明: 1) 对于像 JoyX 这样的游戏杆轴, OutputVar 将设置为介于 0 和 100 之间的浮点数, 作为该轴活动范围的百分率, 表明游戏杆的位置. 可以通过 SetFormat 改变数字的格式. 此 测试脚本 可以用来分析游戏杆. 2) 当 KeyName 为 JoyPOV 时, 获取的值将介于 0 和 35900 之间. 许多游戏杆使用下列近似的 POV 值: |
KeyName | 此参数可以是键盘中任何的单个字符或来自 按键列表 中键名的其中一个, 例如鼠标/游戏杆 (尽管在 Windows 95/98/Me 系统中通常无法检测到鼠标按钮的状态). 示例: B, 5, LWin, RControl, Alt, Enter, Escape, LButton, MButton, Joy1. 或者, 可以指定一个显式的虚拟按键代码例如 vkFF. 只有在一个按键没有指定名称这样少见的情况下, 这种方法才有用.它的虚拟按键代码可以通过 按键列表页面 底部的步骤来确定. |
Mode | 当获取游戏杆状态时, 此参数被忽略. 如果省略, 默认的模式是获取按键的逻辑状态. 这是操作系统和活动窗口所认为的按键处于的状态, 但不一定和它的物理状态一样. 或者, 可以指定这些字母的其中一个: P: 获取物理状态 (例如用户是否实际按住了按键). 在 Windows Me/98/95 系统下, 一个按键的物理状态 (Mode = P) 总是和它的逻辑状态一致. 在 Windows NT/2000/XP 或更高版本的系统中, 如果没有安装键盘和/或鼠标钩子的话, 按键或鼠标按钮的物理状态通常和逻辑状态一致(有时逻辑状态可能比物理状态滞后). 在安装了钩子的情况下, 它将准确反映出用户是否按下了按键或鼠标按钮(只要在脚本正在执行时它被按下).您可以通过 KeyHistory 命令或菜单项确定您脚本中是否使用了钩子. 通过添加 #InstallKeybdHook 和/或 #InstallMouseHook 指令到脚本中您可以强制安装钩子. T: 获取切换状态 (仅对于可以切换的按键才有效例如 Capslock, Numlock, Scrolllock, 和 Insert). 获取的值为 D 表明按键是打开的, 而 U 表明它是关闭的 (不过 GetKeyState() 函数当打开时返回 true (1), 关闭时返回 false (0)). 获取 Insert 按键的切换状态可能只工作在 Windows 2000/XP 或更高版本的系统中. 在 Windows 9x 系统中, 获取切换状态可能不太可靠. 例如, 按下按键后, 可能需要等到显示了一个和脚本关联的窗口例如 MsgBox 后, 才能正确获取它的新状态. |
为了等待按键或鼠标/游戏杆按钮进入一种新的状态, 通常用 KeyWait 来代替 GetKeyState 的循环.
带有不常见键盘驱动的系统更新按键的状态可能比较慢, 尤其是像 Capslock 这样的按键的开关状态. 在按键状态改变后立即对其状态进行检查的脚本可以提前使用 Sleep 命令, 以使系统有时间更新按键的状态.
关于使用 GetKeyState 并与游戏杆有关的例子, 请参看 游戏杆重映射页面 和 游戏杆到鼠标的映射脚本.
GetKeyState(), KeyWait, Key List, Joystick remapping, KeyHistory, #InstallKeybdHook, #InstallMouseHook
; Basic examples: GetKeyState, state, RButton ; Right mouse button. GetKeyState, state, Joy2 ; The second button of the first joystick. GetKeyState, state, Shift if state = D MsgBox At least one Shift key is down. else MsgBox Neither Shift key is down. GetKeyState, state, CapsLock, T ; D if CapsLock is ON or U otherwise. state := GetKeyState("Capslock", "T") ; True if CapsLock is ON, false otherwise. ; Remapping example (this is only for illustration because it would be easier to use ; the built-in remapping feature): ; In the following hotkey, the mouse button is kept held down while NumpadAdd is ; down, which effectively transforms NumpadAdd into a mouse button. This method ; can also be used to repeat an action while the user is holding down a key or button: *NumpadAdd:: MouseClick, left,,, 1, 0, D ; Hold down the left mouse button. Loop { Sleep, 10 GetKeyState, state, NumpadAdd, P if state = U ; The key has been released, so break out of the loop. break ; ... insert here any other actions you want repeated. } MouseClick, left,,, 1, 0, U ; Release the mouse button. return ; Example: Make joystick button behavior depend on joystick axis position. joy2:: GetKeyState, joyx, JoyX if joyx > 75 MsgBox Action #1 (button pressed while joystick was pushed to the right). else if joyx < 25 MsgBox Action #2 (button pressed while joystick was pushed to the left). else MsgBox Action #3 (button pressed while joystick was centered horizontally). return ; See the joystick remapping page and the Joystick-To-Mouse script for other examples.