pythonraspberry-piraspbianmotion-detection

Python to simulate keyboard strokes


Could i take a motion sensor that is attached to the raspberry pi (running Raspbian) that when the sensor is tripped that it would send a keyboard stoke.

i am hoping that the keyboard stroke would control a application.

looking for this to be done in python, but other suggestions would be greatly appreciated


Solution

  • Yes you can detect when the motion sensor is triggered and make it send a keyboard stroke using the python-uinput module.

    example below in python

    import uinput
    # set up keystroke input
    device = uinput.Device([uinput.KEY_TAB])
    while True:
        triggered_sensor = get_sensor_method()
        if triggered_sensor:
            device.emit_click(uinput.KEY_TAB)
    

    This is one suggestion and I have not tested it either. The links below might help you in searching for alternative solutions in python.

    Generate keyboard events

    How to generate keyboard keypress events through Python?

    how to open a program in python and send keystrokes?