I have a 2 files in Visual Studio Code. "main.py" and "g_Global.py". "g_Global.py" has this code:
import msvcrt
def key():
if msvcrt.kbhit() and msvcrt.getch() == chr(27).encode():
u = False
in "main.py" it has:
import g_Global as g
while x.lower() == 'n' and u == True:
g.clear()
print(y)
y = input('Write a line to add. > ')
g.key()
if u == False:
break
...but when I run the code, when I press "esc" nothing happens. Any ideas?
When you access u
from key()
, u
is a local variable. You must add global u
to the beginning to access the global variable. Even then, variables in one source file cannot be modified from another source file. You must combine the two source files (move key()
to the other source file).
Also, although I'm not familiar with msvcrt
, the documentation says that getchr
blocks until there is a key to read. So maybe just get rid of the input()
and kbhit()
? It might not pick up on escape since it is a special key, though.
Sample: (I can't test since I'm not on MSVC)
import msvcrt
while u:
if msvcrt.getch() == chr(27).encode():
u = False