SavinaRoja / PyUserInput

A module for cross-platform control of the mouse and keyboard in python that is simple to install and use.
GNU General Public License v3.0
1.07k stars 244 forks source link

Multiple key tap #14

Open pikusinski opened 11 years ago

pikusinski commented 11 years ago

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?

SavinaRoja commented 11 years ago

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.

pikusinski commented 11 years ago

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 commented 11 years ago

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 =)

pepijndevos commented 11 years ago

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

pikusinski commented 11 years ago

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

SavinaRoja commented 11 years ago

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.

pikusinski commented 11 years ago

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

SavinaRoja commented 11 years ago

@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
pikusinski commented 11 years ago

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!

lowssy commented 6 years ago

Why i do get this error? :cry:

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