I am having an issue in my python script where the for loop repeat itself 2 times sometimes. Like it should write the letters 1 time each and sometimes (I have seen that it is mostely before a ' ')it write it 2 times.
from pynput.keyboard import Key, Controller
import time
partition = '35 66 67 88 89 77 655 6 35 66 67 88 89 77 65 6 35 66 67 88 90 4 4 090 6 67 889 0 6 68 77 86 7 67 889 0 6 68 77 86 7 0 4 000 09 9 8 787 6 9 8 787 6 67 8 90 987 890 9 89 0 98 787 66 75 6 67 8 78 989 098 6 67 890 4 69 8 7 0 4 000 09 9 8 787 6'
enter code here
note = ''
temps = 0.1
keyboard = Controller()
print('Starting')
for i in range(3, 0, -1):
print(i)
time.sleep(1)
print('Go')
for i in partition:
while i == '0' or i == '1' or i == '2' or i == '3' or i == '5' or i == '6' or i == '7' or i == '8' or i == '9':
if i == '1':
note = '&'
if i == '2':
note = 'é'
if i == '3':
note = '"'
if i == '5':
note = "("
if i == '6':
note = '-'
if i == '7':
note = 'è'
if i == '8':
note = '_'
if i == '9':
note = 'ç'
if i == '0':
note = 'à'
keyboard.press(note)
time.sleep(0.1)
keyboard.release(note)
time.sleep(temps)
break
while i == '4':
note = "'"
keyboard.press(Key.shift_l)
keyboard.press(note)
time.sleep(0.1)
keyboard.release(note)
keyboard.release(Key.shift_l)
time.sleep(temps)
break
while i == ' ':
time.sleep(0.04)
break
print(note)
print('Réussi')
(Alot of the variables are wrote in french but this a no importance) The code serve to play a song (the serie of number at the top) in a game, Raft, on a piano, so i have to assign each number to a key of the keyboard. you can use the code on your computer, because the key pressed is wrote in the terminal with the print(note) My problem is that it don't play correctly and repeat the same key sometimes.
So can you help me get it to work properly?
The code is a bit messy, so sorry in advance. (I started python two weeks ago)
Thanks a lot to people that will help!
CodePudding user response:
It doesnt press any button twice you just didnt change the value of the NOTE varuable when the partition was " " so (because every time the for loop runs.. you have it print the note) it printed the previous NOTE value a second time.
I have put 2 # on the line i added.. remove them for it to work
from pynput.keyboard import Key, Controller
import time
partition = '35 66 67 88 89 77 655 6 35 66 67 88 89 77 65 6 35 66 67 88 90 4 4 090 6 67 889 0 6 68 77 86 7 67 889 0 6 68 77 86 7 0 4 000 09 9 8 787 6 9 8 787 6 67 8 90 987 890 9 89 0 98 787 66 75 6 67 8 78 989 098 6 67 890 4 69 8 7 0 4 000 09 9 8 787 6'
enter code here
note = ''
temps = 0.1
keyboard = Controller()
print('Starting')
for i in range(3, 0, -1):
print(i)
time.sleep(1)
print('Go')
for i in partition:
while i == '0' or i == '1' or i == '2' or i == '3' or i == '5' or i == '6' or i == '7' or i == '8' or i == '9':
if i == '1':
note = '&'
if i == '2':
note = 'é'
if i == '3':
note = '"'
if i == '5':
note = "("
if i == '6':
note = '-'
if i == '7':
note = 'è'
if i == '8':
note = '_'
if i == '9':
note = 'ç'
if i == '0':
note = 'à'
keyboard.press(note)
time.sleep(0.1)
keyboard.release(note)
time.sleep(temps)
break
while i == '4':
note = "'"
keyboard.press(Key.shift_l)
keyboard.press(note)
time.sleep(0.1)
keyboard.release(note)
keyboard.release(Key.shift_l)
time.sleep(temps)
break
while i == ' ':
time.sleep(0.04)
#note = " "#
break
print(note)
print('Réussi')