为什么 Visual Studio 在自动热键之前捕获关键事件?
我最近切换到德沃夏克键盘布局作为一个实验。过渡过程中最困难的部分之一是处理热键。大多数热键在设计时都考虑到了 QWERTY,更糟糕的是,热键似乎非常受肌肉记忆的限制。
我没有重新学习所有热键,而是编写了一个自动热键脚本,当 Ctrl、Alt 或 Win< 时,将 Dvorak 布局转换回 QWERTY。 /kbd> 键与其他键一起按下。除了 Visual Studio '08 之外,它在我尝试过的所有地方都运行良好。似乎在自动热键可以翻译之前击键就被捕获了。
为什么会发生这种情况以及如何解决这个问题?
下面是我的脚本的摘录(从头开始):
; control + letter
^;::^z
^q::^x
^j::^c
^k::^v
更新: 该脚本在新安装的 ahk、vs08 和 coderush 的 Win7 上运行良好。我遇到问题的机器运行的是 vista。关于如何进一步诊断有什么想法吗?
更新 2: 该脚本在 Vista 和 2010 beta 2 上运行良好。似乎只适用于 08 + vista。今晚要尝试全新安装 vs08。
I recently switched to the Dvorak keyboard layout as a bit of an experiment. One of the most difficult parts of the transition has been dealing with hot-keys. Most hot-keys are designed with QWERTY in mind and, to make matters worse, hot-keys seem to be extremely muscle memory bound.
Rather than relearn all the hot-keys, I've written an autohotkey script to translate the Dvorak layout back to QWERTY when the Ctrl, Alt, or Win keys are pressed in conjunction with other keys. It works beautifully everywhere I've tried, except Visual Studio '08. It seems keystrokes are being caught before autohotkey can translate them.
Why is this happening and how do I fix this?
Below is an excerpt (from the start) of my script:
; control + letter
^;::^z
^q::^x
^j::^c
^k::^v
Update: The script works fine on Win7 with ahk, vs08, and coderush freshly installed. The machine I'm having trouble with is running vista. Any thoughts on how to further diagnose?
Update 2: The script works fine with Vista and 2010 beta 2. Seems to be something with just vs 08 + vista. Gonna try a fresh install of vs08 tonight.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
啊哈!我已经弄清楚了。如果 ahk 和目标应用程序没有在相同的权限(或用户)下运行,ahk 将无法正确拦截/模拟键盘事件。就我而言,Visual Studio 以管理员(提升)权限运行,而 ahk 脚本以当前登录用户身份运行。
以下任一方法解决了该问题:
Aha! I've figured it out. If ahk and the target app are not running under the same privileges (or user) ahk won't intercept/simulate keyboard events properly. In my case, visual studio was run with administrator (elevated) privileges while the ahk script was run as the currently logged on user.
Either of the following solved the problem:
只是想在OP自己找到的解决方案中添加几点。
1) 问题不在于 AHK 和 VS 以不同权限运行 - 只是在非管理员模式下运行的脚本创建的热键无法在应用程序上工作在admin模式下运行,但如果相反也没有问题。
2)不一定需要编译脚本,只需将 autohotkey.exe 设置为在管理模式下运行(这就是我所做的),或者创建特定脚本的快捷方式并将其设置为始终在管理模式下运行。
(顺便说一句,只是指出,运行 AHK 脚本的编译版本没有性能增益,因为代码仍然被解释 - 只是现在解释器嵌入到创建的可执行文件中)
Just want to add a couple of points to solution found by the OP himself.
1) The problem is not with AHK and VS running with different permissions - its just that hotkeys created by a script running in a non-admin mode wouldn't work on applications running in the admin mode, but there would be no problem if it's the other way round.
2) There is no need to compile the script necessarily, just set autohotkey.exe to run in the admin mode (that's what I do), or alternatively create a shortcut to the particular script and set it to always run in admin mode.
(btw, just to point out, there is no performance gain by running a compiled version of an AHK script, because the code is still interpreted - its just that now the interpreter is embedded in the executable created)
这是由于名为用户界面权限隔离 (UIPI) 的安全功能,该功能是 User 的一部分帐户控制 (UAC)。
常见问题解答中列出了几种解决方法:
我通常不建议以管理员身份运行脚本来解决此问题,因为它会产生意外或不需要的副作用。例如,脚本使用
Run
启动的任何程序也将以管理员身份运行。该脚本还将对各种文件夹(例如 Program Files)拥有不必要的写入权限。一些糟糕的代码(从某处复制粘贴的恶意代码,或者有错误的代码)可能会以这种方式造成更大的损害。当然,我也不推荐最后两种选择。只剩下使用 UI 访问运行,可以按照上面的描述启用和使用它。
This is due to a security feature called User Interface Privilege Isolation (UIPI), which is part of User Account Control (UAC).
There are several workarounds listed in the FAQ:
I generally do not recommend running a script as administrator to work around this issue, as it has side-effects that might be unexpected or undesired. For example, any program that the script launches with
Run
will also run as administrator. The script will also have unnecessary write permission to various folders, such as Program Files. A bit of bad code (malicious code copy-pasted from somewhere, or code with a bug) could do more damage this way.Of course, I do not recommend the last two options either. That leaves only Run with UI Access, which can be enabled and used as described above.
显然有一个解决方法。
来自文档 Program.htm#Installer_uiAccess。
Lexikos 的论坛主题
摘录:
ahk 文件的下载链接在论坛上已损坏,但我在 Github 上找到了它: 启用UIAccess.ahk
Apparently there is a workaround for this.
From the docs Program.htm#Installer_uiAccess.
Forum thread by Lexikos
Excerpt:
The download link to the ahk file is broken on the forum but I found it on Github: EnableUIAccess.ahk