-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton.py
More file actions
61 lines (46 loc) · 1.42 KB
/
Copy pathbutton.py
File metadata and controls
61 lines (46 loc) · 1.42 KB
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
# -*- coding: utf-8 -*-
import platform
# for CM3588
if platform.machine() == 'aarch64':
import time
import struct
from threading import Thread
is_pressed = False
def wait_for_event():
global is_pressed
with open(f'/dev/input/by-path/platform-gpio-keys-event', "rb") as event:
while True:
raw_data = event.read(24)
data = struct.unpack('4IHHI', raw_data)
if data[4] == 1 and data[6] == 1:
is_pressed = True
time.sleep(0.1)
button_thread = Thread(target=wait_for_event)
button_thread.start()
def is_key_pressed():
global is_pressed
to_return = is_pressed
is_pressed = False
return to_return
# for development
elif platform.machine() == 'x86_64':
from pynput import keyboard
class KeyListener:
def __init__(self):
self.key_pressed = False
self.listener = keyboard.Listener(on_press=self.on_press)
self.listener.start()
def on_press(self, _):
self.key_pressed = True
def is_key_pressed(self):
if self.key_pressed:
self.key_pressed = False
return True
return False
listener = KeyListener()
def is_key_pressed():
return listener.is_key_pressed()
# for other platforms
else:
def is_key_pressed():
return False