Advertisement
King0fGamesYami

k400r keyboard bindings for pi 3

Oct 25th, 2017
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.60 KB | None | 0 0
  1. import os
  2. import keyboard
  3. import time
  4.  
  5. shutdown_time = 0
  6. shutting_down = False
  7.  
  8. def mycallback( keyEvent ):
  9.     global shutdown_time
  10.     global shutting_down
  11.     global home_pushed
  12.     if keyEvent.event_type == keyboard.KEY_UP:
  13.         print 'released'
  14.         shutting_down = False #abort shutdown procedure
  15.     elif keyEvent.scan_code == 142: #the "PC Power" button on the keyboard
  16.         if shutting_down: #if they power button is being held in...
  17.             print 'waiting for time to be up'
  18.             if keyEvent.time - shutdown_time > 5: #...and more than 5s have elapsed...
  19.                 if keyboard.is_pressed( 'alt' ):
  20.                     print 'rebooting'
  21.                     os.system( 'sudo shutdown -r now' )
  22.                 elif keyboard.is_pressed( 'shift' ):
  23.                     print( 'logging out' )
  24.                     os.system( 'lxde-logout' )
  25.                 else:
  26.                     print 'shutting down'
  27.                     os.system( 'sudo shutdown -h now' ) #...shutdown!
  28.         else: #if the button has just been pressed...
  29.             print 'logging power button pressed time'
  30.             shutdown_time = keyEvent.time #store the time it was initially pressed
  31.             shutting_down = True #store that the button has been pressed and is now being held down
  32.     elif keyEvent.scan_code == 172: #the "Home" button on the keyboard
  33.         print 'showing desktop'
  34.         keyboard.press( 'control' )
  35.         keyboard.press( 'alt' )
  36.         keyboard.press_and_release( 'd' )
  37.         keyboard.release( 'control' )
  38.         keyboard.release( 'alt' )
  39.     elif keyEvent.scan_code == 164: #the 'pause/play' button aka F8
  40.         print 'pausing/playing omxplayer'
  41.     else:
  42.         print keyEvent.scan_code
  43.         print keyEvent.name
  44.         print keyEvent.event_type
  45.         print keyEvent.time
  46.  
  47. keyboard.hook( mycallback )
  48. while True:
  49.     time.sleep(10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement