-3

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

danwright
  • 93
  • 2
  • 5

3 Answers3

2

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?

Community
  • 1
  • 1
tockards
  • 189
  • 6
  • Link-only answers are not useful, see http://meta.stackexchange.com/q/225370/248731. If you've found another post that answers the OP's question, please flag to close as a duplicate. – jonrsharpe Oct 05 '15 at 11:09
  • haven’t found a post that duplicates it but links that can help OP. – tockards Oct 05 '15 at 11:43
1

Since you don't say anything about the application I assume you don't have the possibility to change it or to simply replace it with a different application which can receive requests for actions via other means, e.g. it has a CLI. If you do have the freedom to replace the app, then choose something which is command-line based and just execute the app with the correct parameters when the sensor triggers.

I'm also assuming that the application receiving the keys runs on the rpi. Using the xdotool and a bit of experimenting you could find a window attribute which uniquely defines the GUI element of your application that needs to receive the keystroke. Then you could execute xdotool with the right set of parameters whenever the sensor event occurs. This will take some effort but it is totally doable.

Look at this link for an actual "project" that uses xdotool on the raspbian.

user1514631
  • 1,183
  • 1
  • 9
  • 14
-1

There is also a dtoverlay function that you can add to the config.txt file that does this as well. However, it has some complications. For example, I have a motion censor that is supposed to activate the backspace key but every time my motion activates I get a runaway occurrence of the keystroke so that it presses the backspace key repeatedly until the motion is activated again then stops briefly then starts again. I gained some control over it by running the circuitry through a nand gate with a 1k resistor but I still get phantom keystrokes sometimes when the room has been completely empty where the motion detector is located. It could be the motion censor I'm using though as it was really cheap generic. In case you were wondering here is the code:

    sudo nano /boot/config.txt
    #Add this line at the end
    #Keystroke on PIR
    dtoverlay=gpio-key,gpio=17,keycode=14, 
    label="KEY_BACKSPACE"