Advertisement
Guest User

Untitled

a guest
Apr 19th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.28 KB | None | 0 0
  1. import asyncio, evdev
  2. import itertools
  3. import subprocess
  4.  
  5. KEY_SPACE = 57
  6. KEY_SYM = 162
  7.  
  8. keys_pressed = {
  9.     KEY_SPACE: 0,
  10.     KEY_SYM: 0,
  11. }
  12.  
  13. layouts = ['us', 'ru']
  14. cmd = "dconf write /desktop/lipstick-jolla-home/layout '{layout_code}'"
  15.  
  16. @asyncio.coroutine
  17. def layout_switcher(device):
  18.     layouts_pool = itertools.cycle(layouts)
  19.  
  20.     layout_code = next(layouts_pool)    
  21.     print('Current layout is {}'.format(layout_code))
  22.     set_layout(layout_code)
  23.  
  24.     while True:
  25.         events = yield from device.async_read()
  26.         for event in events:
  27.             if event.type == evdev.ecodes.EV_KEY:
  28.                 # Sym + space
  29.                 if event.code in (KEY_SPACE, KEY_SYM):
  30.                     keys_pressed[event.code] = event.value
  31.                    
  32.         if keys_pressed[KEY_SPACE] and keys_pressed[KEY_SYM]:
  33.             layout_code = next(layouts_pool)            
  34.             print('Switching keyboard layout to {}'.format(layout_code))
  35.             set_layout(layout_code)
  36.            
  37. def set_layout(layout_code):
  38.     layout_cmd = cmd.format(layout_code=layout_code).split()
  39.     subprocess.call(layout_cmd)
  40.  
  41. keybd = evdev.InputDevice('/dev/input/event3')
  42. asyncio.async(layout_switcher(keybd))
  43.  
  44. loop = asyncio.get_event_loop()
  45. loop.run_forever()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement