为什么 Visual Studio 在自动热键之前捕获关键事件?

发布于 2024-08-14 08:04:44 字数 619 浏览 9 评论 0原文

我最近切换到德沃夏克键盘布局作为一个实验。过渡过程中最困难的部分之一是处理热键。大多数热键在设计时都考虑到了 QWERTY,更糟糕的是,热键似乎非常受肌肉记忆的限制。

我没有重新学习所有热键,而是编写了一个自动热键脚本,当 CtrlAltWin< 时,将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

茶花眉 2024-08-21 08:04:44

啊哈!我已经弄清楚了。如果 ahk 和目标应用程序没有在相同的权限(或用户)下运行,ahk 将无法正确拦截/模拟键盘事件。就我而言,Visual Studio 以管理员(提升)权限运行,而 ahk 脚本以当前登录用户身份运行。

以下任一方法解决了该问题:

  • 以当前用户身份运行 vs 和 ahk
  • 编译脚本并以管理员身份运行 vs 和编译后的应用程序

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:

  • Running both vs and ahk as the current user
  • Compiling the script and running both vs and the compiled app as administrator
在梵高的星空下 2024-08-21 08:04:44

只是想在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)

青瓷清茶倾城歌 2024-08-21 08:04:44

这是由于名为用户界面权限隔离 (UIPI) 的安全功能,该功能是 User 的一部分帐户控制 (UAC)。

常见问题解答中列出了几种解决方法:

如何解决由用户帐户控制 (UAC) 引起的问题?< /p>

常见的解决方法如下:

  • 在 AutoHotkey 设置中启用将“使用 UI 访问权限运行”添加到上下文菜单选项。通过从“开始”菜单重新运行 AutoHotkey 安装程序,可以启用或禁用此选项,而无需重新安装 AutoHotkey。启用后,右键单击脚本文件并选择使用 UI 访问运行来启动脚本文件,或使用命令行,例如 "AutoHotkeyU32_UIA.exe" "Your script.ahk"(但包括完整路径)。
  • 以管理员身份运行脚本。请注意,这还会导致脚本启动的任何程序以管理员身份运行,并且可能要求用户在启动脚本时接受批准提示。
  • 停用本地安全策略“以管理员批准模式运行所有管理员”(不推荐)。
  • 完全禁用 UAC。不建议这样做,并且在 Windows 8 或更高版本上不可行。


我通常不建议以管理员身份运行脚本来解决此问题,因为它会产生意外或不需要的副作用。例如,脚本使用 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:

How do I work around problems caused by User Account Control (UAC)?

Common workarounds are as follows:

  • Enable the Add 'Run with UI Access' to context menus option in AutoHotkey Setup. This option can be enabled or disabled without reinstalling AutoHotkey by re-running AutoHotkey Setup from the Start menu. Once it is enabled, launch your script file by right-clicking it and selecting Run with UI Access, or use a command line like "AutoHotkeyU32_UIA.exe" "Your script.ahk" (but include full paths).
  • Run the script as administrator. Note that this also causes any programs launched by the script to run as administrator, and may require the user to accept an approval prompt when launching the script.
  • Disable the local security policy "Run all administrators in Admin Approval Mode" (not recommended).
  • Disable UAC completely. This is not recommended, and is not feasible on Windows 8 or later.

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.

悲念泪 2024-08-21 08:04:44

显然有一个解决方法。

来自文档 Program.htm#Installer_uiAccess
Lexikos 的论坛主题

摘录:

启用UIAccess

修改 AutoHotkey.exe 以允许脚本在启用 UAC 的情况下执行以下操作:

与管理程序窗口交互,无需以管理员身份运行脚本。
使用发送播放。
有限制;请在使用此脚本之前阅读该帖子。

ahk 文件的下载链接在论坛上已损坏,但我在 Github 上找到了它: 启用UIAccess.ahk

Apparently there is a workaround for this.

From the docs Program.htm#Installer_uiAccess.
Forum thread by Lexikos

Excerpt:

EnableUIAccess

Modifies AutoHotkey.exe to allow scripts to do the following even while UAC is enabled:

Interact with windows of administrative programs without running the script as administrator.
Use SendPlay.
There are limitations; please read the post before using this script.

The download link to the ahk file is broken on the forum but I found it on Github: EnableUIAccess.ahk

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文