0

I created a script that plays the piano and the up and down arrows change the transpose. But when it's used within a game, the up, down, left, right keys don't work.

my script:

def play_midi(notes, tempo):
    import keyboard
    import shocurasy
    for variables in notes:
        exec(f'note{variables} = 0')

    transpose = 0
    keys = ['1', 'shift + 1', '2', 'shift + 2', '3', '4', 'shift + 4', '5', 'shift + 5', '6', 'shift + 6',
            '7', '8', 'shift + 8', '9', 'shift + 9', '0', 'q', 'shift + q', 'w', 'shift + w', 'e', 'shift + e',
            'r', 't', 'shift + t', 'y', 'shift + y', 'u', 'i', 'shift + i', 'o', 'shift + o', 'p', 'shift + p',
            'a', 's', 'shift + s', 'd', 'shift + d', 'f', 'g', 'shift + g', 'h', 'shift + h', 'j', 'shift + j',
            'k', 'l', 'shift + l', 'z', 'shift + z', 'x', 'c', 'shift + c', 'v', 'shift + v', 'b', 'shift + b',
            'n', 'm']

    for id_note in range(len(notes)):
        shocurasy.tempo(tempo[id_note])
        if eval(f'note{notes[id_note]}') == 0:
            exec(f'note{notes[id_note]} = 1')
            if notes[id_note] < 24:
                set = notes[id_note] - 24
            elif notes[id_note] > 84:
                set = notes[id_note] - 85
            else:
                set = 0

            while True:
                if transpose < set:
                    transpose += 1
                    keyboard.press_and_release('up')
                elif transpose > set:
                    transpose -= 1
                    keyboard.press_and_release('down')
                else:
                    break

            print(f'note value = {notes[id_note]}')
            if notes[id_note] < 24:
                keyboard.press(keys[0])
            elif notes[id_note] > 84:
                keyboard.press(keys[-1])
            else:
                keyboard.press(keys[notes[id_note]-24])

        else:
            exec(f'note{notes[id_note]} = 0')
            if notes[id_note] < 24:
                keyboard.release(keys[0])
            elif notes[id_note] > 84:
                keyboard.release(keys[-1])
            else:
                keyboard.release(keys[notes[id_note] - 24])

I've already tested it with pydirectinput, but it's too much slow to run the function. I need something I can do quickly.

Lotexiu
  • 1
  • 1
  • you can read one character from standard input (see [here](https://stackoverflow.com/questions/510357/how-to-read-a-single-character-from-the-user) and compare its ASCII code with arrow-key ASCII codes. – Nima Afshar May 31 '22 at 14:48

0 Answers0