Avatar ·

Python: how to block keys for everything except the keyboard module?

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

I have a program that should substitute certain characters and character combinations when certain keys are pressed:

import keyboardwhile True:    hotkey = keyboard.read_hotkey(suppress=False)    if hotkey == 'j+k':        keyboard.write('j + k')

(I'm not using keyboard.add_hotkey() because in the future my plans involve not specific hotkeys, but checking for the presence of certain keys in hotkey. But this doesn't change the situation: add_hotkey() behaves the same way) I expect that when running this program, when I simultaneously press j

Avatar ·

Solution using the extended Euclidean algorithm. The method using exponentiation requires computing Euler's totient function.

Edit: Following @Eduard's hint, removed extra steps to get a positive result (x may be negative after the Euclidean algorithm), which are not required in Python

def gcdExtended(a, b):    if a == 0 :        retu b,0,1    gcd,x1,y1 = gcdExtended(b%a, a)    x = y1 - (b//a) * x1    y = x1    retu gcd,x,ym, a = map(int, input                                

Log in to leave an answer

Интересные статьи