2022-03-06 19:48:20 +03:00
from pynput . keyboard import Key , Controller
from pynput . keyboard . _xorg import KeyCode
2022-03-15 07:13:46 +03:00
from pynput . _util . xorg import display_manager
2022-03-05 22:10:16 +03:00
import os
import sys
import socket
2022-06-23 11:06:30 +03:00
from Xlib . ext . xtest import fake_input
from Xlib import X
2022-06-24 07:01:22 +03:00
import Xlib
2022-03-05 22:10:16 +03:00
2022-03-15 07:13:46 +03:00
KeyCode . _from_symbol ( " \0 " ) # test
2022-03-06 19:48:20 +03:00
2022-06-23 11:06:30 +03:00
2022-03-15 07:13:46 +03:00
class MyController ( Controller ) :
def _handle ( self , key , is_press ) :
""" Resolves a key identifier and sends a keyboard event.
: param event : The * X * keyboard event .
: param int keysym : The keysym to handle .
"""
2022-06-27 04:19:38 +03:00
keysym = self . _keysym ( key )
keycode = self . _display . keysym_to_keycode ( keysym )
with display_manager ( self . _display ) as dm :
Xlib . ext . xtest . fake_input (
dm ,
Xlib . X . KeyPress if is_press else Xlib . X . KeyRelease ,
keycode )
2022-03-15 07:13:46 +03:00
keyboard = MyController ( )
2022-03-05 22:10:16 +03:00
server_address = sys . argv [ 1 ]
if not os . path . exists ( os . path . dirname ( server_address ) ) :
os . makedirs ( os . path . dirname ( server_address ) )
try :
os . unlink ( server_address )
except OSError :
if os . path . exists ( server_address ) :
raise
server = socket . socket ( socket . AF_UNIX , socket . SOCK_STREAM )
server . bind ( server_address )
server . listen ( 1 )
clientsocket , address = server . accept ( )
2022-06-23 11:06:30 +03:00
os . system ( ' chmod a+rw %s ' % server_address )
2022-03-05 22:10:16 +03:00
print ( " Got pynput connection " )
2022-03-06 19:48:20 +03:00
def loop ( ) :
global keyboard
buf = [ ]
while True :
data = clientsocket . recv ( 1024 )
if not data :
print ( " Connection broken " )
2022-03-05 22:10:16 +03:00
break
2022-03-06 19:48:20 +03:00
buf . extend ( data )
while buf :
n = buf [ 0 ]
n = n + 1
if len ( buf ) < n :
break
msg = bytearray ( buf [ 1 : n ] ) . decode ( " utf-8 " )
buf = buf [ n : ]
if len ( msg ) < 2 :
continue
if msg [ 1 ] == " \0 " :
2022-03-15 07:17:07 +03:00
keyboard = MyController ( )
2022-03-06 19:48:20 +03:00
print ( " Keyboard reset " )
continue
if len ( msg ) == 2 :
name = msg [ 1 ]
else :
name = KeyCode . _from_symbol ( msg [ 1 : ] )
2022-03-15 07:13:46 +03:00
if str ( name ) == " <0> " :
2022-03-06 19:48:20 +03:00
continue
2022-03-08 07:08:18 +03:00
try :
if msg [ 0 ] == " p " :
keyboard . press ( name )
else :
keyboard . release ( name )
except Exception as e :
print ( e )
2022-03-06 19:48:20 +03:00
loop ( )
2022-03-05 22:10:16 +03:00
clientsocket . close ( )
server . close ( )