summaryrefslogtreecommitdiff
path: root/foreign/client_handling/keystroke.py
blob: 03047f0b8ba2a8621b8e079c91bff78d60a675b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import threading
import time

from pynput.keyboard import Key, Controller as KeyboardController
from pynput.mouse import Button, Controller as MouseController

from foreign.parse.crash_exception_handling import *


@crash_exception_handling
def keystroke_action(inject):
  keyboard = KeyboardController()
  mouse = MouseController()

  for command in inject:
    command = command.split(' ')
    c_type, c_data = command[0], command[1]

    if c_type == 'press': 
      try:
        c_data = eval(f'Key.{c_data}')
      except:
        pass
      finally:
        keyboard.press(c_data)
        keyboard.release(c_data)
    elif c_type == 'hold':
      try:
        c_data = eval(f'Key.{c_data}')
      except:
        pass
      finally:
        keyboard.press(c_data)
    elif c_type == 'release':
      try:
        c_data = eval(f'Key.{c_data}')
      except:
        pass
      finally:
        keyboard.release(c_data)
    elif c_type == 'type':
      keyboard.type(' '.join(command[1:]))
    elif c_type == 'position':
      mouse.position = [int(position) for position in c_data.split(',')]
    elif c_type == 'move':
      move_positions = [int(position) for position in c_data.split(',')]
      mouse.move(move_positions[0], move_positions[1])
    elif c_type == 'mhold':
      mouse.press(eval(f'Button.{c_data}'))
    elif c_type == 'mrelease':
      mouse.release(eval(f'Button.{c_data}'))
    elif c_type == 'click':
      mouse.press(eval(f'Button.{c_data}'))
      mouse.release(eval(f'Button.{c_data}'))
    elif c_type == 'dclick':
      mouse.click(eval(f'Button.{c_data}'), 2)
    elif c_type == 'scroll':
      scroll_positions = [int(position) for position in c_data.split(',')]
      mouse.scroll(scroll_positions[0], scroll_positions[1])
    elif c_type == 'sleep':
      time.sleep(float(c_data))
    else:
      raise Exception('Invalid keystroke command')


def keystroke(inject):
  threading.Thread(target=keystroke_action, args=(inject,), daemon=True).start()
  return {'message': 'Keystroke thread started', 'text_mode': 'primary', 'text_extras': {'point': True}}