Advertisement
najjah

Working Push2Talk AutoKey Snippet

Sep 28th, 2022 (edited)
720
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.99 KB | Source Code | 0 0
  1. # This AutoKey script was made to allow a push-to-talk feature in applications that don't offer it natively such as Zoom.
  2. # Do not forget to define your custom parameters, including the expected mute/unmute action, and to match the hotkey variable with the trigger hotkey defined for this script on AutoKey itself.
  3. # P.S. I kept my debugging code commented out just for reference.
  4.  
  5. # Enter script code
  6. import os
  7.  
  8. import evdev
  9.    
  10. # Defining some custom parameters
  11. lockfile = '.p2t.lockfile'          # => You may choose whatever filename you like here. More details below.
  12. device_path = '/dev/input/event6'   # => Be sure to check what input device corresponds to your keyboard with `python -m evdev.evtest`.
  13. hotkey = 'KEY_F1'                   # => This hotkey should be the same one that triggers your script.
  14. hold_action = 2
  15. expected_action = '<alt>+A'         # => This is the expected key combination to trigger mute/unmute on the target app.
  16.  
  17. # Also, you can setup AutoKey to only trigger this script when your target app is open, based on the window title. In my case, I set it up with regex /*ReuniĆ£o Zoom/.
  18. #P.S. all lines that are entirely commented out were used for debugging and can be removed if you like.
  19.  
  20. try:
  21.   open(lockfile)                                 # => If the lockfile exists, script does nothing. This is important because keeping a key pressed in fact sends several hits like you were typing it very quickly.
  22. except FileNotFoundError:
  23.   with open(lockfile, 'w'):                      # => Else, the script creates the lockfile and starts its magic.
  24.     #print('Starting operation')
  25.     #counter = 0
  26.    
  27.     device = evdev.InputDevice(device_path)      # => EvDev is used to access our keyboard.
  28.     keyboard.send_keys(expected_action)          # => Here we send our "unmute" command to the target app.
  29.    
  30.     for event in device.read_loop():             # => And here we start monitoring the keyboard for keypress events.
  31.       if event.type == evdev.ecodes.EV_KEY:      # => EvDev sends different types of events and this part ensures we'll check only for keypress events.
  32.         category = evdev.categorize(event)       # => This is used to view the necessary event details.
  33.         #print(category.keycode, type(category.keycode), category.keystate)
  34.        
  35.         if category.keycode != hotkey or category.keystate != hold_action: # => `keycode` is the pressed key itself and keystate represents one of the three possible actions: down, up or hold.
  36.           break                                  # => If we detect that we are no longer holding the hotkey, break the loop.
  37.          
  38.         #counter += 1
  39.         #print('Loop ', counter)
  40.         #print(category.keycode, type(category.keycode), category.keystate)
  41.    
  42.   keyboard.send_keys(expected_action)            # => Here we issue the "mute" command to the target app.
  43.   os.remove(lockfile)                            # => And remove the lockfile so as we can use the Push2Talk script again.
  44.   #print('Ending operation')
Advertisement
Comments
  • najjah
    70 days
    Comment was deleted
Add Comment
Please, Sign In to add comment
Advertisement