最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - scanning keyboard input during input - Stack Overflow

programmeradmin0浏览0评论

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

Share Improve this question asked Feb 6 at 13:44 IllTime00qwIllTime00qw 1245 bronze badges 4
  • 1 I am not sure if I am getting you correctly. If your code is at 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 the threading library. – Daraan Commented Feb 6 at 13:48
  • no i want like , user should be able to save program in two line and not wait like 5 lines to get a prompt to save , user can press esc and set line_count to 0 and save in two lines only – IllTime00qw Commented Feb 6 at 13:51
  • This question is similar to: how to escape an input() in python after pressing escape. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – David Commented Feb 6 at 14:23
  • Also: stackoverflow.com/q/56622170/328193 or stackoverflow.com/q/63840829/328193 etc. – David Commented Feb 6 at 14:24
Add a comment  | 

1 Answer 1

Reset to default -2

After 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
发布评论

评论列表(0)

  1. 暂无评论