Python node for publishing keyboard events
I am trying to build a node in python that publishes key press as strings onto a topic. I am able to print out the keypress on the console, but when I publish it I get this error :(
RuntimeError: maximum recursion depth exceeded
This is all of my code:
class KeyboardPublisher:
def __init__(self):
# logging the node start
rospy.loginfo("Starting node keyboard_publisher")
# Initialise the node
rospy.init_node("keypress_publisher", anonymous=True)
# Create a publisher to the keypress topic
self.keyboard_publisher = rospy.Publisher('/keypress', String, queue_size=1)
def publish_keypress(self, key_press):
self.publish_keypress(key_press)
def on_press(self, key):
print(key) #<-- this prints out the key press and then crashes
self.publish_keypress(key)
def on_release(self, key):
print('{0} release'.format(
key))
if key == Key.esc:
# Stop listener
return False
def keyboard_listener(self):
# Collect events until released
with Listener(
on_press=self.on_press,
on_release=self.on_release) as listener:
listener.join()
if __name__ == '__main__':
keyboard_publisher = KeyboardPublisher()
keyboard_publisher.keyboard_listener()
try:
rospy.spin()
except rospy.ROSInterruptException:
print("Stopping keyboard_publisher"