forked from yuvaramsingh94/Ros-Keyboard-Input
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeypress.py
More file actions
42 lines (35 loc) · 1.14 KB
/
keypress.py
File metadata and controls
42 lines (35 loc) · 1.14 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
#!/usr/bin/env python
#import getch
import rospy
from std_msgs.msg import String #String message
from std_msgs.msg import Int8
from pynput.keyboard import Key, Listener
################################
# created by yuvaram
#yuvaramsingh94@gmail.com
# modified by Tongrui Li
################################\
pub = rospy.Publisher('key',Int8,queue_size=10) # "key" is the publisher name
rospy.init_node('keypress',anonymous=True)
rate = rospy.Rate(20)#try removing this line ans see what happens
def on_press(key):
if hasattr(key, "char") and key.char == "w":
print("Entering on press event for w")
rospy.loginfo(str(1))# to print on terminal
pub.publish(1)#to publish
elif hasattr(key, "char") and key.char == "e":
rospy.loginfo(str(2))# to print on terminal
pub.publish(2)#to publish
print('{} pressed'.format(
key))
def on_release(key):
print('{0} release'.format(
key))
if key == Key.esc:
# Stop listener
return False
# Collect events until released
with Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()