Skip to content

Commit 5627e2d

Browse files
committed
Change keyboard module to pynput for the M1 macbook
1 parent 20bdf6e commit 5627e2d

File tree

3 files changed

+50
-39
lines changed

3 files changed

+50
-39
lines changed

neopia/_keyboard.py

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,45 @@
1616
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
1717
# Boston, MA 02111-1307 USA
1818

19-
import os
20-
from . import keyboard
19+
from pynput.keyboard import Key, Listener
2120

2221

2322
class Keyboard(object):
24-
BACKSPACE = 'delete'
25-
TAB = 'tab'
26-
ENTER = 'enter'
27-
F1 = 'f1'
28-
F2 = 'f2'
29-
F3 = 'f3'
30-
F4 = 'f4'
31-
F5 = 'f5'
32-
F6 = 'f6'
33-
F7 = 'f7'
34-
F8 = 'f8'
35-
F9 = 'f9'
36-
F10 = 'f10'
37-
F11 = 'f11'
38-
F12 = 'f12'
23+
BACKSPACE = Key.backspace
24+
TAB = Key.tab
25+
ESC = Key.esc
3926

40-
HOME = 'home'
41-
UP = 'up'
42-
PAGE_UP = 'page up'
43-
LEFT = 'left'
44-
RIGHT = 'right'
45-
END = 'end'
46-
DOWN = 'down'
47-
PAGE_DOWN = 'page down'
48-
SPACE = 'space'
49-
DELETE = 'forward delete'
27+
ENTER = Key.enter
28+
F1 = Key.f1
29+
F2 = Key.f2
30+
F3 = Key.f3
31+
F4 = Key.f4
32+
F5 = Key.f5
33+
F6 = Key.f6
34+
F7 = Key.f7
35+
F8 = Key.f8
36+
F9 = Key.f9
37+
F10 = Key.f10
38+
F11 = Key.f11
39+
F12 = Key.f12
40+
41+
HOME = Key.home
42+
UP = Key.up
43+
PAGE_UP = Key.page_up
44+
LEFT = Key.left
45+
RIGHT = Key.right
46+
END = Key.end
47+
DOWN = Key.down
48+
PAGE_DOWN = Key.page_down
49+
SPACE = Key.space
50+
DELETE = Key.delete
51+
52+
@staticmethod
53+
def _on_pressed(key):
54+
print(key)
55+
return key
5056

5157
@staticmethod
52-
def read():
53-
event = keyboard.read_event()
54-
if event.event_type == keyboard.KEY_DOWN:
55-
return event.name
56-
58+
def read(handler):
59+
with Listener(on_press = handler) as listener:
60+
listener.join()

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
setup(
44
name="neopia",
5-
version="0.2.4",
5+
version="0.3.0",
66
author="RoboticsWare",
77
author_email="[email protected]",
88
description="Python library for NEOPIA Neobot",
99
url="https://github.com/roboticsware/pylib_neobot.git",
1010
download_url="https://github.com/roboticsware/pylib_neobot/archive/refs/heads/master.zip",
1111
long_description=open("README.md").read(),
1212
long_description_content_type="text/markdown",
13-
install_requires=["pyserial", "websocket-client", "keyboard"],
13+
install_requires=["pyserial", "websocket-client", "pynput"],
1414
packages=find_packages(exclude=["examples", "tests"]),
1515
python_requires=">=3",
1616
zip_safe=False,

test/test.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,26 @@
3333
# wait(500)
3434

3535
# case6) Moving control by direction keys on the keyboard
36-
# while True:
37-
# key = Keyboard.read()
38-
36+
# def on_press(key):
3937
# if key == Keyboard.UP:
40-
# n.motor_move('forward')
38+
# print('up')
39+
# n.motor_move('forward')
4140
# elif key == Keyboard.DOWN:
41+
# print('down')
4242
# n.motor_move('backward')
4343
# elif key == Keyboard.LEFT:
44+
# print('left')
4445
# n.motor_move('left')
4546
# elif key == Keyboard.RIGHT:
47+
# print('right')
4648
# n.motor_move('right')
47-
# elif key == ' ':
49+
# elif key == Keyboard.SPACE:
50+
# print('stop')
4851
# n.motor_move('stop')
52+
# elif key == Keyboard.ESC:
53+
# return False
54+
55+
# Keyboard.read(on_press)
4956

5057
# case7) Move forth and back with speed 30% during 1s and stop
5158
# n.motor_rotate('both', 'forward', '30')

0 commit comments

Comments
 (0)