Skip to content

Commit

Permalink
client: server: add hid keyboard
Browse files Browse the repository at this point in the history
  • Loading branch information
doronz88 committed Mar 6, 2022
1 parent 8370b79 commit 8b40d5c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/rpcclient/rpcclient/darwin/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from rpcclient.darwin import objective_c_class
from rpcclient.darwin.consts import kCFNumberSInt64Type, kCFNumberDoubleType, CFStringEncoding, kCFAllocatorDefault
from rpcclient.darwin.fs import DarwinFs
from rpcclient.darwin.hid import Hid
from rpcclient.darwin.ioregistry import IORegistry
from rpcclient.darwin.location import Location
from rpcclient.darwin.media import DarwinMedia
Expand Down Expand Up @@ -69,6 +70,7 @@ def __init__(self, sock, sysname: str, hostname: str, port: int = None):
self.xpc = Xpc(self)
self.syslog = Syslog(self)
self.time = Time(self)
self.hid = Hid(self)

@property
def modules(self) -> typing.List[str]:
Expand Down
65 changes: 65 additions & 0 deletions src/rpcclient/rpcclient/darwin/hid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import contextlib
import logging
import time
from enum import Enum

from rpcclient.darwin.consts import kCFAllocatorDefault
from rpcclient.exceptions import BadReturnValueError

logger = logging.getLogger(__name__)


class Keycode(Enum):
VOLUME_UP = 0xe9
VOLUME_DOWN = 0xea
HOME = 0x40
POWER = 0x30


class Hid:
def __init__(self, client):
self._client = client

def send_double_home_button_press(self):
self.send_home_button_press()
self.send_home_button_press()

def send_home_button_press(self):
self.send_key_press(Keycode.HOME.value)

def send_power_button_press(self, interval: int = 0):
self.send_key_press(Keycode.POWER.value, interval=interval)

def send_volume_down_button_press(self, interval: int = 0):
self.send_key_press(Keycode.VOLUME_DOWN.value, interval=interval)

def send_volume_up_button_press(self, interval: int = 0):
self.send_key_press(Keycode.VOLUME_UP.value, interval=interval)

def send_key_press(self, key_code: int, interval: int = 0):
self.send_keyboard_event(key_code, True)
if interval:
time.sleep(interval)
self.send_keyboard_event(key_code, False)

def send_keyboard_event(self, key_code: int, down: bool):
event = self._client.symbols.IOHIDEventCreateKeyboardEvent(kCFAllocatorDefault,
self._client.symbols.mach_absolute_time(),
0x0c, key_code, down, 0)
self.dispatch(event)

@contextlib.contextmanager
def create_hid_client(self):
client = self._client.symbols.IOHIDEventSystemClientCreate(0)

if not client:
raise BadReturnValueError('IOHIDEventSystemClientCreate() failed')

try:
yield client
finally:
self._client.symbols.CFRelease(client)

def dispatch(self, event):
with self.create_hid_client() as hid_client:
self._client.symbols.IOHIDEventSystemClientDispatchEvent(hid_client, event)
8 changes: 8 additions & 0 deletions src/rpcserver/ents.plist
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
<true/>
<key>com.apple.locationd.status</key>
<true/>
<key>com.apple.private.hid.client.admin</key>
<true/>
<key>com.apple.private.hid.client.event-dispatch</key>
<true/>
<key>com.apple.private.hid.client.event-filter</key>
<true/>
<key>com.apple.private.hid.client.event-monitor</key>
<true/>
<key>task_for_pid-allow</key>
<true/>
<key>com.apple.private.tcc.allow</key>
Expand Down

0 comments on commit 8b40d5c

Please sign in to comment.