操作系统升级后模拟的按键命令不起作用 [英] Simulated key command not working since OS upgrade

查看:24
本文介绍了操作系统升级后模拟的按键命令不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我考虑将其发布到Ask Different,但我认为它属于这里,因为主要关注代码.

I considered posting this to Ask Different but I thought it belonged here due to being mainly focused on code.

我在我的 Mac 上创建了一个用于切换空间的宏,自从升级到 Sierra 后,以下 AppleScript 不再有效.有人知道有什么变化吗?

I have created a macro on my mac for switching spaces, and since upgrading to Sierra the following AppleScript is no longer working. Does anyone know if something has changed?

tell application "System Events" to key code 124 using control down

tell application "System Events" to key code 123 using control down

这是在终端中运行时的输出(注意^[[1;5D):

This is the output when running in terminal (note the ^[[1;5D):

14:16 isaac@Isaac ~ $ osascript -e 'tell application "System Events" to key code 123 using control down'
^[[1;5D14:18 isaac@Isaac ~ $ ;5D

当通过 AppleScript 编辑器运行时,没有任何反应.

And when running via AppleScript Editor, nothing happens.

推荐答案

是的,这是一个错误.

要使用 Control 键模拟某些全局快捷键,该命令需要 fn 键(Sierra 的解决方法).

To simulate some global shortcut with the Control key, the command needs the fn key (a workaround for Sierra).

fn 键无法与 AppleScript 的 key code 命令一起使用,但可以使用 Core Graphics 框架的方法在 Python 脚本中.

It's not possible to use the fn key with the AppleScript's key code command, but it's possible with the methods of the Core Graphics framework in a Python script.

这是模拟此快捷方式的脚本 --> (右箭头 + 控制),您可以在 终端 中运行该脚本(在 sh、bash 或任何类似的外壳)

Here's the script to simulate this shortcut --> (Right Arrow + Control), you can run the script in the Terminal (in a sh, bash or any similar shell)

/usr/bin/python -c 'import time; import Quartz.CoreGraphics as QCG; e = QCG.CGEventCreateKeyboardEvent(None, 124, True); QCG.CGEventSetFlags(e, (QCG.kCGEventFlagMaskControl | QCG.kCGEventFlagMaskSecondaryFn)); QCG.CGEventPost(QCG.kCGHIDEventTap, e); time.sleep(0.1); QCG.CGEventSetType(e, QCG.kCGEventKeyUp); QCG.CGEventPost(QCG.kCGHIDEventTap, e)'

<小时>

这是要在脚本编辑器"应用程序中测试的 AppleScript:


Here's an AppleScript to test in the "Script Editor" application:

--  For switching spaces, 124 = the Right Arrow key, use 123 for the Left Arrow key
do shell script "/usr/bin/python -c 'import time; import Quartz.CoreGraphics as QCG; e = QCG.CGEventCreateKeyboardEvent(None, 124, True); QCG.CGEventSetFlags(e, (QCG.kCGEventFlagMaskControl | QCG.kCGEventFlagMaskSecondaryFn)); QCG.CGEventPost(QCG.kCGHIDEventTap, e); time.sleep(0.1); QCG.CGEventSetType(e, QCG.kCGEventKeyUp); QCG.CGEventPost(QCG.kCGHIDEventTap, e)'"

这篇关于操作系统升级后模拟的按键命令不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆