In Python and Windows environment how to supress certain key press and send some other key event for it
J. Clarke
j.clarke.873638 at gmail.com
Sun Apr 23 08:27:49 EDT 2017
In article <mailman.46.1491151672.2961.python-list at python.org>,
i.am.songoku at gmail.com says...
>
> Hi All,
>
> I was trying to build a VIM like shortcuts in windows. For example,
>
> IF i press CAPSLOCK & h: It should "{Left}" move one to left. If CAPSLOCK &
> b: It should "{Ctrl Down}{Left}{Ctrl Up}" move one word left etc.
>
> I was successful in sending the key event. I used libraries like,
>
> 1. pynput
> 2. keyboard
>
> for the same.
>
> But i was unable to supress the Initial key event (ex: Caplock & h)
>
> I tried using the keyboard to listen for key press events and supress them
> as below.
>
> >>>>>
> import keyboard
>
> def move_one_left():
> """
> Wrapper for CAPS LOCK + b. The function sends LEFT ARROW Event
> :return:
> """
> keyboard.send(75)
>
> def move_one_word_left():
> """
> Wrapper for CAPS LOCK + b. The function sends LEFT ARROW Event
> :return:
> """
> keyboard.send('ctrl+left')
>
> def main():
> """
> This is the main loop which reads the key pressed.
> According to the hotkey registered the function hooked is called.
> The corresponding function will be the wrapped combination send back.
> For example: CAPS + b is wrapped to Moving to left.
> The main loop exits when Ctrl + c is done. So that is not registered.
> :return:
> """
> try:
> # Start of the main loop
> # Registering the hotkeys
> # CAPS LOCK + H
> keyboard.add_hotkey('caps lock+h', move_one_left, suppress=True)
> # CAPS LOCK + B
> keyboard.add_hotkey('caps lock+b', move_one_word_left,
> suppress=True)
>
> while True:
> # Read the key pressed
> print (keyboard.read_key())
> except KeyboardInterrupt:
> print("User has exited the program")
>
> if __name__ == "__main__":
> main()
>
> <<<<<
>
>
> This is working for sending the event for key press but the initial
> keypress is also being send. The supress=True is not working.
>
> Am I doing something wrong or is there any better way to supress the
> initial key event and send another key event in place of that.
<https://pypi.python.org/pypi/keyboard>
Check the "known limitations" section.
More information about the Python-list
mailing list