yonubear
Posts: 90
Joined: Thu Sep 12, 2013 11:11 pm

uinput help

Wed Mar 02, 2016 3:25 am

I am hoping someone here is familiar with uinput and can help me out with an issue I have a simple peice of code but I can't seem to sort out the issue or rather I don't understand the sysntax enough to sort it out.

My code

Code: Select all

#!/usr/bin/python
import uinput
import sys
my_key = str(sys.argv[0])
device.emit(my_key, 1) # Press.
device.emit(my_key, 0) # Release.
the command triggering the script
./test.py A

the error from the terminal

Code: Select all

Traceback (most recent call last):
  File "./test.py", line 7, in <module>
    device.emit(my_key, 1) # Press.
NameError: name 'device' is not defined
Thanks,
Yonu

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: uinput help

Wed Mar 02, 2016 12:41 pm

Well, it clearly says what the issue is "name 'device' is not defined" - you have not defined or imported the name 'device'. Looks to me you are missing the creation of the uinput.Device object:

Code: Select all

#!/usr/bin/python
import uinput
import sys
my_key = str(sys.argv[0])

device = uinput.Device(...)

device.emit(my_key, 1) # Press.
device.emit(my_key, 0) # Release.
...now what you need to put in stead of the '...' is another question.
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

yonubear
Posts: 90
Joined: Thu Sep 12, 2013 11:11 pm

Re: uinput help

Wed Mar 02, 2016 2:16 pm

ok now to totally sound stupid any idea what I should have for ... as I can't find may references for device equals. The only ones I find have keys in it.

Yonu

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: uinput help

Wed Mar 02, 2016 2:27 pm

Well, I can't say I know this library and the docs (http://tjjr.fi/sw/python-uinput/) are fairly sparse so not sure. Looks to me like you need to initialise it with a list of keys you are planning to send ...and your script looks like it could take any keys, so... Maybe you can just start with a couple of keys you plan to try - just grab from the docs:

Code: Select all

device = uinput.Device([
        uinput.KEY_E,
        uinput.KEY_H,
        uinput.KEY_L,
        uinput.KEY_O,
        ])
and only use those as arguments for now.
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

yonubear
Posts: 90
Joined: Thu Sep 12, 2013 11:11 pm

Re: uinput help

Thu Mar 03, 2016 1:20 am

well I made the recommended changes however I get this error now.

Code: Select all

Traceback (most recent call last):
  File "./test.py", line 13, in <module>
    device.emit(my_key, 1) # Press.
  File "/usr/local/lib/python2.7/dist-packages/uinput/__init__.py", line 204, in emit
    ev_type, ev_code = event
ValueError: too many values to unpack
here is the code I have

Code: Select all

!/usr/bin/python
import uinput
import sys
my_key = "uinput."+str(sys.argv[1])
print my_key + '/n'
device = uinput.Device([
        uinput.KEY_E,
        uinput.KEY_H,
        uinput.KEY_L,
        uinput.KEY_O,
        ])

device.emit(my_key, 1) # Press.
device.emit(my_key, 0) # Release.

yonubear
Posts: 90
Joined: Thu Sep 12, 2013 11:11 pm

Re: uinput help

Sat Mar 19, 2016 12:30 am

I got my code working however I have a stupid question if i trigger a key press will it act as if the key is held down until it gets the same key with a 0?

Return to “Python”