如何接受来自箭头键的输入,或接受方向输入? [英] How do I accept input from arrow keys, or accept directional input?

查看:19
本文介绍了如何接受来自箭头键的输入,或接受方向输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是 xy 问题,但我正在尝试构建一个基于内核的文本编辑器,类似于 vimnano,并且我知道如何使用转义字符清除屏幕,然后重印,我可以让它接受字符,但我不确定如何让它接受导航箭头输入.我认为它们有 ASCII 值,但显然没有.有没有办法使用箭头,或者我必须像 vim 那样制作导航模式和插入模式?

This may be an xy problem, but I'm trying to to build a kernel based text editor, similar to vim or nano, and I know how to use the escape chars to clear the screen, then reprint, I can have it accept characters, but I'm not sure how to get it to accept arrow inputs for navigation. I thought there were ASCII values for them, but apparently not. Is there a way to use the arrows, or do I have to make a navigation mode and insert mode like vim?

我也曾短暂地玩过 curses,但这令人望而却步,因为据我所知,必须为它打开一个全新的窗口,这与单一的愿景不兼容我拥有的终端窗口.

I've also briefly played with curses, but that was prohibitive because, as I understood, a whole new window had to be opened for it and this is not compatible with the vision of a single terminal window that I had.

curses 也令人望而却步,因为它清除了我不想要的窗口.

curses was also prohibitive because it cleared the window, which I didn't want.

推荐答案

我最终使用了 this question,并修改 __init__ 语句,使其最多接受列表中的 3 个字符.

I wound up using the code from this question, and modifying the __init__ statement so that it accepted up to 3 characters in a list.

import sys

class _Getch:
   """Gets a single character from standard input.  Does not echo to the
screen."""
   def __init__(self):
      self.impl = _GetchUnix()

   def __call__(self):# return self.impl()
      charlist = []
      counter = 0
      for i in range(3):
         try:charlist.append(self.impl())
         except:pass
         if charlist[i] not in [chr(27),chr(91)]:#TODO sort out escape vs arrow duh use len()
            break
         if len(charlist) > 1:
            if charlist == [chr(27),chr(27)]:
               break
      if len(charlist) == 3:
         if charlist[2] == 'a'
            return 'u-arr'
         if charlist[2] == 'b'
            return 'd-arr'
         if charlist[2] == 'c'
            return 'r-arr'
         if charlist[2] == 'd'
            return 'l-arr'
      if len(charlist == 2):
         if charlist == [chr(27),chr(27)]
            return chr(27)
      if len(charlist == 1)
         return charlist[0]
      return ''

class _GetchUnix:
   def __init__(self):
      import tty, sys

   def __call__(self):
      import sys, tty, termios
      fd = sys.stdin.fileno()
      old_settings = termios.tcgetattr(fd)
      try:
         tty.setraw(sys.stdin.fileno())
         ch = sys.stdin.read(1)
      finally:
         termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
      return ch

这使我能够从编辑器的键盘获取箭头键以及所有其他字符和转义序列.它使Getch"类不是严格的 get char 克隆,因为它返回一个字符串,但它最终变得更有用.

This allowed me to get arrow keys as well as all other characters and escape sequences from the keyboard for the editor. It made the "Getch" class not strictly a get char clone because it returns a string, but it wound up being much more useful.

这篇关于如何接受来自箭头键的输入,或接受方向输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆