Skip to content

Commit

Permalink
Update use_keyboard to handle headless environments
Browse files Browse the repository at this point in the history
  • Loading branch information
lalalune committed Jul 16, 2023
1 parent 9d81086 commit 318a3c9
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions agentloop/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import threading

from pynput import keyboard


def stop(loop_data):
"""
Expand Down Expand Up @@ -103,7 +101,7 @@ def loop(steps, stepped=False, loop_data=None):
break


def use_keyboard(loop_data, input_key=keyboard.Key.space):
def use_keyboard(loop_data, input_key=None):
"""
Listen for a specified key press, and when detected, step the loop
Expand All @@ -115,6 +113,17 @@ def use_keyboard(loop_data, input_key=keyboard.Key.space):
Returns:
loop_data: The updated loop dictionary with the newly created listener
"""
keyboard = None
try:
from pynput import keyboard as _keyboard
keyboard = _keyboard
except ImportError:
raise ImportError(
"pynput not installed. Please install it with `pip install pynput`"
)

if input_key is None:
input_key = keyboard.Key.space

def on_press(key):
if key == input_key:
Expand Down

0 comments on commit 318a3c9

Please sign in to comment.