PyUserInput icon indicating copy to clipboard operation
PyUserInput copied to clipboard

Multiple key tap

Open pikusinski opened this issue 12 years ago • 10 comments

IDK if it already exists, if it does please tell me how.

Anyway. It would be really nice if you could tap multiple keys at once, for instance ALT + TAB. Also, is there a way to listen for specific strings? For instance, if I write "close", could the program catch that string and then execute a function?

pikusinski avatar Oct 07 '13 06:10 pikusinski

This will do a combined key press:

#Create an Alt+Tab combo
k.press_key(k.alt_key)
k.tap_key(k.tab_key)
k.release_key(k.alt_key)

If you want to listen for a series of keys or combinations of keys, create your own sub-class of PyKeyboardEvent and give it your own custom key_press method. What system are you using? This may be an area that I might need to develop in the code.

SavinaRoja avatar Oct 07 '13 18:10 SavinaRoja

I understand. Pressing keys needs a releasing, and tapping just taps. That's nice to know.

I will do so.

At work I use windows, where i will use pyUserInput for repetative tasks, hence the need for a listener. On my spare time I use linux and will probably make a similar thing.

pikusinski avatar Oct 07 '13 18:10 pikusinski

Quick question. How am i supposed to listen too a specific key combination, for instance the word "start". I'm not very skilled in Python yet and could use a friendly push =)

pikusinski avatar Oct 08 '13 12:10 pikusinski

You could append incoming events to a string and check if it ends with "start"

pepijndevos avatar Oct 08 '13 12:10 pepijndevos

Some sample code would be really nice. For instance how to bind the keyboard event in a class and perhaps use a function. Would really appreciate the help. Because, the problem i have right now is to even access the event class

pikusinski avatar Oct 08 '13 12:10 pikusinski

Hey @pikusinski I don't want to leave you hanging... I am currently working on improving how to work with listening for key actions so I haven't worked on examples. I'll let you now as soon as I can about how to take advantage of PyKeyboardEvent.

SavinaRoja avatar Oct 10 '13 00:10 SavinaRoja

Thanks for the info! All in all, it's a great project and I'm using it quite a bit now.

pikusinski avatar Oct 10 '13 06:10 pikusinski

@pikusinski I just put in some code that seems to work well enough for Linux using PyKeyboardEvent if you wanted to try it out. Here's a bit of example code to demonstrate

from pykeyboard import PyKeyboardEvent

class KeyTest(PyKeyboardEvent):
    def tap(self, keycode, character, press):
        """The keycode represents a lower level view of the
           key event, it is available to provide simpler extension
           for unusual special keys.

           If the key event is associated with a printable, then
           that will be the value of character. If it does not
           then it will be given a standard name such as BackSpace
           or KP_5 (number 5 on the keypad); if the key was not
           recognized then character will be None.

           press will be a boolean value where True is the press
           of a key and False is a release."""

        #Print the keycode and character if the key is pressed
        if press:
            print(keycode, character)

        #Show the modifier states if Ctrl+Shift+M is pressed
        #The Shift state is implicit with a capital 'M'
        if press and character == 'M' and self.modifiers['Control']:
            print(self.modifiers)

key_tester = KeyTest()
key_test.start()  # The 'Escape' key should stop the listener

SavinaRoja avatar Nov 06 '13 00:11 SavinaRoja

Wow that's great. Thank you for taking your time!

I have been using pyUserInput for quite a while and I must say that it's great!

pikusinski avatar Nov 06 '13 07:11 pikusinski

Why i do get this error? :cry:

key_test.start() # The 'Escape' key should stop the listener NameError: name 'key_test' is not defined

lowssy avatar Mar 30 '18 17:03 lowssy