I am trying to make a simple , text writing program in python here is what i have wrote , i just started this like 10 minutes ago Please forgive me i have not wrote any error handling , also i am a beginner
main.py
import keyboard
no_of_lines = 5
line_count = no_of_lines
lines = ""
while line_count > 0:
lines += input() + "\n"
line_count -= 1
if keyboard.is_pressed("esc"):
print("lines set to 0")
line_count=0
if line_count == 0:
print("last line")
try:
inc_line = int(input("0 or extra line::"))
except ValueError:
inc_line = 0
if inc_line > 0:
no_of_lines += inc_line
line_count += inc_line
print("Press 'S' to save.")
while True:
if keyboard.is_pressed("s"):
print("User pressed save. Saving...")
filename = input("filename >")
path = input("path >")
with open(f"{path}{filename}","w") as file:
file.write(lines)
break
The problem i am facing is that the "ESC" key is not being properly scanned and i tested it multiple times i had to like , when i open program at the same moment when i pressed ESC like super fast then only it recognized the key press , otherwise during the input it is not recognizing , it is showing as ^[
also
when i press 's' to save its auto skipping filename block and saving all files with only s
I am trying to make a simple , text writing program in python here is what i have wrote , i just started this like 10 minutes ago Please forgive me i have not wrote any error handling , also i am a beginner
main.py
import keyboard
no_of_lines = 5
line_count = no_of_lines
lines = ""
while line_count > 0:
lines += input() + "\n"
line_count -= 1
if keyboard.is_pressed("esc"):
print("lines set to 0")
line_count=0
if line_count == 0:
print("last line")
try:
inc_line = int(input("0 or extra line::"))
except ValueError:
inc_line = 0
if inc_line > 0:
no_of_lines += inc_line
line_count += inc_line
print("Press 'S' to save.")
while True:
if keyboard.is_pressed("s"):
print("User pressed save. Saving...")
filename = input("filename >")
path = input("path >")
with open(f"{path}{filename}","w") as file:
file.write(lines)
break
The problem i am facing is that the "ESC" key is not being properly scanned and i tested it multiple times i had to like , when i open program at the same moment when i pressed ESC like super fast then only it recognized the key press , otherwise during the input it is not recognizing , it is showing as ^[
also
when i press 's' to save its auto skipping filename block and saving all files with only s
1 Answer
Reset to default -2After reviewing your code, it appears you have used the wrong attribute for the escape key. It should be as follows:
if keyboard.is_pressed("Esc"):
This should hopefully solve your issue, assuming there aren't any other errors.
Final Code:
import keyboard
no_of_lines = 5
line_count = no_of_lines
lines = ""
while line_count > 0:
lines += input() + "\n"
line_count -= 1
if keyboard.is_pressed("Esc"):
print("lines set to 0")
line_count=0
if line_count == 0:
print("last line")
try:
inc_line = int(input("0 or extra line::"))
except ValueError:
inc_line = 0
if inc_line > 0:
no_of_lines += inc_line
line_count += inc_line
print("Press 'S' to save.")
while True:
if keyboard.is_pressed("s"):
print("User pressed save. Saving...")
filename = input("filename >")
path = input("path >")
with open(f"{path}{filename}","w") as file:
file.write(lines)
break
input
then it is blocked for that moment and not other code is processed, i.e.keyboard.is_pressed
. You maybe want to look into thethreading
library. – Daraan Commented Feb 6 at 13:48esc
and setline_count
to 0 and save in two lines only – IllTime00qw Commented Feb 6 at 13:51