In Step 20 of the tutorial you mentioned, all that is happening is that the script listens for keyboard input on the raspberry pi, and then sends that over Bluetooth. If you simply want to instantiate the Bluetooth connection and then send keystrokes programmatically and finally exit you just need to replace the call to
kb.event_loop(bt) with something like
bt.send_input(self.state) and of course manually set the self.state structure. As described in Step 17, the input reports are made of 9 bytes and prefixed with a byte indicating that the remaining bytes are an input report (
0xA1). You will need to instantiate the report with 9 values and do the following things:
- assign 0x01 to the first byte in the array, which will basically tell the bluetooth device that you are working with a keyboard input report.
- the second byte is used to specify any modifier keys (alt, shift, ctrl etc.) which is useful if you need to hold down shift to sprint in the game.
- the third byte you shouldn't touch.
- The other 6 bytes you should use to specify which keys you want to simulate in form of Bluetooth key constants provided in the keymap.py file.
All you need to do is to pass an array with this content to
Here is an example of what the structure could look like:
self.state = [
0xA1, # This is an input report
0x01, # Usage report = Keyboard
# Bit array for Modifier keys
[0, # Right GUI - (usually the Windows key)
0, # Right ALT
0, # Right Shift
0, # Right Control
0, # Left GUI - (again, usually the Windows key)
0, # Left ALT
1, # Left Shift is pressed
0], # Left Control
0x00, # Vendor reserved
0x04, # The W key is pressed
The hex array would be something like this in hex:
This would simulate that you hold left shift and the
w key. You should probably use
keymap.modkey(ecodes.KEY_W) for readability.
For further information I found this github repository which I think could be useful for you. Also you should read the documentation for python-evdev here.
Unfortunately I can't test this as I haven't got a raspberry pi available now, but I think you should be able to figure it out, good luck!