PageRenderTime 24ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/api/library/python/iterm2/docs/examples/function_key_tabs.rst

https://github.com/gnachman/iTerm2
ReStructuredText | 66 lines | 52 code | 14 blank | 0 comment | 0 complexity | 152a66d83431837f9df3d266d0b5f96b MD5 | raw file
  1. :orphan:
  2. .. _function_key_tabs_example:
  3. Function Key Tabs
  4. =================
  5. The script makes it possible to select a tab by pressing a function key. F1 chooses the first tab, F2 the second, etc.
  6. .. code-block:: python
  7. #!/usr/bin/env python3
  8. import asyncio
  9. import iterm2
  10. async def main(connection):
  11. app = await iterm2.async_get_app(connection)
  12. keycodes = [ iterm2.Keycode.F1,
  13. iterm2.Keycode.F2,
  14. iterm2.Keycode.F3,
  15. iterm2.Keycode.F4,
  16. iterm2.Keycode.F5,
  17. iterm2.Keycode.F6,
  18. iterm2.Keycode.F7,
  19. iterm2.Keycode.F8,
  20. iterm2.Keycode.F9,
  21. iterm2.Keycode.F10,
  22. iterm2.Keycode.F11,
  23. iterm2.Keycode.F12 ]
  24. async def keystroke_handler(connection, keystroke):
  25. if keystroke.modifiers == [ iterm2.Modifier.FUNCTION ]:
  26. try:
  27. fkey = keycodes.index(keystroke.keycode)
  28. if fkey >= 0 and fkey < len(app.current_terminal_window.tabs):
  29. await app.current_terminal_window.tabs[fkey].async_select()
  30. except:
  31. pass
  32. pattern = iterm2.KeystrokePattern()
  33. pattern.forbidden_modifiers.extend([iterm2.Modifier.CONTROL,
  34. iterm2.Modifier.OPTION,
  35. iterm2.Modifier.COMMAND,
  36. iterm2.Modifier.SHIFT,
  37. iterm2.Modifier.NUMPAD])
  38. pattern.required_modifiers.extend([iterm2.Modifier.FUNCTION])
  39. pattern.keycodes.extend(keycodes)
  40. async def monitor():
  41. async with iterm2.KeystrokeMonitor(connection) as mon:
  42. while True:
  43. keystroke = await mon.async_get()
  44. await keystroke_handler(connection, keystroke)
  45. # Run the monitor in the background
  46. asyncio.create_task(monitor())
  47. # Block regular handling of function keys
  48. filter = iterm2.KeystrokeFilter(connection, [pattern])
  49. async with filter as mon:
  50. await iterm2.async_wait_forever()
  51. iterm2.run_forever(main)
  52. :Download:`Download<function_key_tabs.its>`