如何在登录窗口mac OS中使用CGEventPost模拟键盘和鼠标事件? [英] How to simulate keyboard and mouse events using CGEventPost in login window mac OS?

查看:623
本文介绍了如何在登录窗口mac OS中使用CGEventPost模拟键盘和鼠标事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个预登录代理,该代理使用CGEventPost来模拟键盘.仅供参考,我正在开发类似于teamviewer的远程控制应用程序.

键盘

CGEventRef keyEvent = CGEventCreateKeyboardEvent( NULL, keyCode, down ) ;
CGEventPost( kCGHIDEventTap, keyEvent ) ;
CFRelease( keyEvent ) ;

鼠标

CGEventRef event = CGEventCreateMouseEvent(eventSource, eventType, mouseLocation, mouseButton );
CGEventPost(kCGHIDEventTap, event);
CFRelease(event);

登录前启动代理

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Label</key>
        <string>my app label</string>
        <key>LimitLoadToSessionType</key>
        <string>LoginWindow</string>
        <key>RunAtLoad</key>
        <true/>
        <key>WorkingDirectory</key>
        <string>My app directory</string>
        <key>ProgramArguments</key>
        <array>
                <string>app absolute path</string>
                <string>service</string>
                <string>myservice</string>
        </array>
        <key>KeepAlive</key>
        <true/>
</dict>
</plist>

CGEventPost无法正常工作,登录后我在控制台日志中得到以下信息

Untrusted apps are not allowed to connect to
Window Server before login.

我已经搜索了铬的遥控器(具有键盘和鼠标模拟功能)的源代码.他们使用CGEventPost作为键盘,但是它可以在登录窗口中使用.

https://cs. chrome.org/chromium/src/remoting/host/input_injector_mac.cc?rcl=0&l=42

他们似乎在特权帮助器工具目录中使用sh文件并用于加载服务,我尝试将服务放入特权帮助器工具中,但事件处理仍然失败.

不推荐使用的API CGPostMouseEvent和CGPostKeyBoardEvent可以正常工作,但真的想知道非不推荐使用的键盘API在Chrome中的工作原理.

解决方案

应该在二进制文件的各个部分中添加一些未记录的(经典的苹果,通过模糊性来确保安全性),以使CGEventPost在LoginWindow Context中运行时能神奇地工作. /p>

如果您使用的是gcc,请将以下内容添加到编译标志中

gcc <YOUR SOURCES AND FLAGS> -sectcreate CGPreLoginApp __CGPreLoginApp /dev/null

如果您使用的是XCode,请将以下内容添加到项目的构建"设置中的其他链接器"标志中:

"-sectcreate"
__CGPreLoginApp
__cgpreloginapp
/dev/null

请参考 https://opensource.apple.com /source/IOHIDFamily/IOHIDFamily-700/IOHIDFamily.xcodeproj/project.pbxproj LDFLAGS的项目文件

I have created a pre-login agent which uses CGEventPost for simulating keyboard. FYI I am developing a remote control app similar to teamviewer.

Keyboard

CGEventRef keyEvent = CGEventCreateKeyboardEvent( NULL, keyCode, down ) ;
CGEventPost( kCGHIDEventTap, keyEvent ) ;
CFRelease( keyEvent ) ;

Mouse

CGEventRef event = CGEventCreateMouseEvent(eventSource, eventType, mouseLocation, mouseButton );
CGEventPost(kCGHIDEventTap, event);
CFRelease(event);

Pre-login launch agent

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Label</key>
        <string>my app label</string>
        <key>LimitLoadToSessionType</key>
        <string>LoginWindow</string>
        <key>RunAtLoad</key>
        <true/>
        <key>WorkingDirectory</key>
        <string>My app directory</string>
        <key>ProgramArguments</key>
        <array>
                <string>app absolute path</string>
                <string>service</string>
                <string>myservice</string>
        </array>
        <key>KeepAlive</key>
        <true/>
</dict>
</plist>

CGEventPost is not working, I get the following in the Console logs after login

Untrusted apps are not allowed to connect to
Window Server before login.

I have searched o chromium's remote control (which has keyboard and mouse simulation working) source code. They use CGEventPost for keyboard, but it works in login window.

https://cs.chromium.org/chromium/src/remoting/host/input_injector_mac.cc?rcl=0&l=42

They seem to use a sh file in privileged helper tools directory and use to to load the service, I tried putting our service in privileged helpers tool, but still the event handling fails.

The Deprecated API CGPostMouseEvent, CGPostKeyBoardEvent work without problem , but would really like to know how non deprecated keyboard API works in chromium.

解决方案

There is undocumented (classic apple , security through obscurity) stuff you should add to binary's sections, in order to make CGEventPost magically work when running in LoginWindow Context.

If you are using gcc, add the following to the compile flags

gcc <YOUR SOURCES AND FLAGS> -sectcreate CGPreLoginApp __CGPreLoginApp /dev/null

If you are using XCode, add the following to Other Linker flags in the Build settings of the project:

"-sectcreate"
__CGPreLoginApp
__cgpreloginapp
/dev/null

Refer https://opensource.apple.com/source/IOHIDFamily/IOHIDFamily-700/IOHIDFamily.xcodeproj/project.pbxproj project file for the LDFLAGS

这篇关于如何在登录窗口mac OS中使用CGEventPost模拟键盘和鼠标事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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