Using my HTC Vive controllers in AutoHotkey

(or: tool assisted smugging)

When I got my HTC Vive Pro I was disappointed at the lack of script-ability when it came to automation or really any kind of programatic hook, and scouring Reddit only found me a tool for using the Oculus Rift through AHK, which seemed unfair especially given it’s extreme inferiority. This greatly annoyed me, given how badly I needed to make a shortcut to start and stop recording in ShareX to assist me in smugging on people who make better financial choices than me, so I made this.

Searching around on Google eventually found me this, a script able to get HTC Vive controller inputs live, and print the events, so I quickly augmented this with the python win32api to emulate keypresses, and much to my surprise it worked perfectly, so far I’ve already completed my initial goal of taking screenshots and videos with ShareX entirely from VR, and will soon move on to making more smugging tools through controller shortcuts.

pressed = {}

def take_press(set_side, set_key, set_value, set_keycode, data):
    if data['side'] == set_side and data[set_key] == set_value and not pressed.get(hex(set_keycode)):
        pressed[hex(set_keycode)] = True
        win32api.keybd_event(set_keycode, 0, 0, 0)
    elif data['side'] == set_side and data[set_key] != set_value and pressed.get(hex(set_keycode)):
        pressed[hex(set_keycode)] = False
        win32api.keybd_event(set_keycode, 0 ,win32con.KEYEVENTF_KEYUP ,0)

def handle_input(data):
    take_press('left', 'grip_button', True, 0x88, data)
    take_press('right', 'grip_button', True, 0x89, data)
    take_press('left', 'trigger', 1, 0x8A, data)
    take_press('right', 'trigger', 1, 0x8B, data)
    take_press('left', 'menu_button', True, 0x8C, data)
    take_press('right', 'menu_button', True, 0x8D, data)
    take_press('left', 'trackpad_pressed', True, 0x8E, data)
    take_press('right', 'trackpad_pressed', True, 0x8F, data)
    take_press('left', 'trackpad_touched', True, 0x92, data)
    take_press('right', 'trackpad_touched', True, 0x93, data)

You can use the virtual keypresses in anything, besides a few applications that won’t recognize it as a shortcut due to the keycodes not being registered to anything.

An example of usage would be this simple AutoHotKey script, which will activate if you press right menu, left grip, and right grip, and will run an awkwardly long key combination you might want to bind in ShareX.

~vk0x88 & ~vk0x89::
    If GetKeyState("vk0x8D") {
        Send, {Control down}
        Send, {Shift down}
        Send, {Alt down}
        Send, {LWin down}
        Send, {p down}
        Send, {p up}
        Send, {Control up}
        Send, {Shift up}
        Send, {Alt up}
        Send, {LWin up}
    }

If you’re actually interested in my finished script, see this gist