Haskell读取原始键盘输入 [英] Haskell read raw keyboard input

查看:157
本文介绍了Haskell读取原始键盘输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Haskell写了一个终端模式程序。我将如何阅读原始按键信息?



特别是,似乎在Haskell之上提供了行编辑功能。如果我做了 getLine ,我似乎可以使用向上箭头来获取上一行,编辑文本,并且只有当我按下Enter时,文本才会变得可见Haskell应用程序本身。



我后来可以读取各个按键,所以我可以自己实现行编辑。






也许我的问题还不清楚。基本上我想要构建像Vi或Emacs(或 Yi )。我已经知道有终端绑定可以让我进行奇妙的控制台模式打印,所以输出端不应该成为问题。我只是寻找一种方法来获得原始按键输入,所以我可以做这样的事情,例如,当用户按下字母K时将K添加到当前文本行,或者当用户将文件保存到磁盘时按Ctrl + S。

解决方案

听起来像您想要readline支持。有几个软件包可以做到这一点,但 haskeline 可能是支持最多的平台最容易使用的软件。

  import Control.Monad.Trans 
import System.Console.Haskeline

类型Repl a = InputT IO a

process :: String - > IO()
process = putStrLn

repl :: Repl()
repl = do
minput< - getInputLine>
case minput
无 - > outputStrLn再见。
只需输入 - > (liftIO $处理输入)>> repl

main :: IO()
main = runInputT defaultSettings repl


I'm writing a terminal-mode program in Haskell. How would I go about reading raw keypress information?

In particular, there seems to be something providing line-editing facilities on top of Haskell. If I do getLine, I seem to be able to use the up-arrow to get previous lines, edit the text, and only when I press Enter does the text become visible to the Haskell application itself.

What I'm after is the ability to read individual keypresses, so I can implement the line-editing stuff myself.


Perhaps my question was unclear. Basically I want to build something like Vi or Emacs (or Yi). I already know there are terminal bindings that will let me do fancy console-mode printing, so the output side shouldn't be an issue. I'm just looking for a way to get at raw keypress input, so I can do things like (for example) add K to the current line of text when the user presses the letter K, or save the file to disk when the user presses Ctrl+S.

解决方案

Sounds like you want readline support. There are a couple of packages to do this, but haskeline is probably the easiest to use with the most supported platforms.

import Control.Monad.Trans
import System.Console.Haskeline

type Repl a = InputT IO a

process :: String -> IO ()
process = putStrLn

repl :: Repl ()
repl = do
  minput <- getInputLine "> "
  case minput of
    Nothing -> outputStrLn "Goodbye."
    Just input -> (liftIO $ process input) >> repl

main :: IO ()
main = runInputT defaultSettings repl

这篇关于Haskell读取原始键盘输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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