Avatar ·

Python: How to block the keys for everything except the Keyboard module?

📁 python, вопрос, боты, ии

I have a program that should substitute certain characters and combinations of symbols when pressing certain keys:

   impport  keyboard  when   true :  housey = keyboard.read_hotkey =  false )   if  hotkey ==  'j + k' :  keyboard.write ( '' j + k ')     

(I do not use keyboard.add_hotkey () , because in the future the plans are not specific hot keys, but checking for certain keys in Hotkey. J + K , say, in a text editor, 'J + K' will be printed. But I get 'jkj + k'. That is, the keys themselves j and k are transmitted to the text editor until the function is triggered. If I do:

  keyboard.block_key ( 'j' ) keyboard.block_key ( 'k' )     

, these keys will not be processed and read_hotkey

the question: is it possible to make certain keys blocked for everything except Listener in Keyboard? That is, so that at the time of the program, these keys could not perform other functions?

upd: I found that with blocked keys, it still works keyboard.is_pressed () , and I had the following code:

= "" " Class = "" Data-Highlighted = ""> Import keyboardkeyboard.block_key ( 'j' ) keyboard.block_key ( 'k' ) white true : if keyboard.is_pressed ( 'j + k' ): keyboard.write ( 'j + k' ) r

the problem of in The fact that when such a code is launched in the background, the keyboard begins to slow down very much (apparently due to a huge number of checks in the cycle). If someone tells how to optimize such a code, it will also be a great solution to the problem.