即使在使用noecho()之后,使用getch()获取的字符也会写入控制台

2024-09-29 23:32:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在编写一个文本编辑器。if userInput == 68行检查用户是否按下左箭头键,我认为代码会运行,但是^[[仍然会打印出来,即使我使用了noecho()。这是密码

#!/usr/bin/env python3

from unicurses import * 
from sys import *

stdscr = initscr()
start_color()
use_default_colors()
noecho()

fileLines = []
fileName = "test.txt"
currentY = 0
currentX = 0 
userInput = ""

for i in range(0, 256):
    curses.init_pair(i + 1, i, -1)

while True:

userInput = getch()

if userInput == 68:
    currentX = currentX-1
    move(currentY, currentX)
elif userInput == 127:
    if currentX != 0:
        currentX = currentX-1
        move(currentY, currentX)
        addstr(" ")
        move(currentY, currentX)
    else:
        continue
else:
    addstr(chr(userInput))
    currentX = currentX+1

endwin()

Tags: 代码fromimport密码moveif检查用户else

热门问题