常见的Lisp,如何掩盖键盘输入 [英] common lisp, how to mask keyboard input

查看:216
本文介绍了常见的Lisp,如何掩盖键盘输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是Common Lisp中的一个Hangman型游戏的控制台程序.第一个玩家输入要由第二个玩家猜测的字符串.我的输入功能在下面---不幸的是,第一个玩家键入的字符仍然可见.

This is a console program in Common Lisp for a Hangman type game. The first player enters a string to be guessed by the second player. My input function is below --- unfortunately the characters typed by the first player remain visible.

使用JavaScript很简单,只需使用密码文本输入框即可.使用VB,使用相同类型的工具很简单.有什么办法可以使用本机Common Lisp函数来做到这一点?

With JavaScript it's simple, just use a password text entry box. With VB it's simple using the same sort of facility. Is there any way to do this using a native Common Lisp function?

谢谢CC.

(defun get-answer ()
  (format t "Enter the word or phrase to be guessed: ~%")
  (coerce (string-upcase (read-line)) 'list))

(defun start-hangman ()
  (setf tries 6)
  (greeting)
  (setf answer (get-answer))
  (setf obscure (get-obscure answer))
  (game-loop answer obscure))

推荐答案

每种实现对此的支持都不同.

Each implementation supports this differently.

您可能想使用 iolib.termios

You might want to use an auxiliary library like iolib.termios or cl-charms (interface to libcurses) if you want a portability layer above different implementations.

我为SBCL找到了一个讨论线程,这是Richard M. Kreuter的实现代码:

I found a discussion thread about it for SBCL, and here is the code for that implementation, from Richard M. Kreuter:

(require :sb-posix)

(defun echo-off ()
  (let ((tm (sb-posix:tcgetattr sb-sys:*tty*)))
    (setf (sb-posix:termios-lflag tm)
      (logandc2 (sb-posix:termios-lflag tm) sb-posix:echo))
    (sb-posix:tcsetattr sb-sys:*tty* sb-posix:tcsanow tm)))

(defun echo-on ()
  (let ((tm (sb-posix:tcgetattr sb-sys:*tty*)))
    (setf (sb-posix:termios-lflag tm)
      (logior (sb-posix:termios-lflag tm) sb-posix:echo))
    (sb-posix:tcsetattr sb-sys:*tty* sb-posix:tcsanow tm)))

因此,这里终于有机会谈论 PROG2 :

And so, here is finally an opportunity to talk about PROG2:

(defun read-silently ()
  (prog2
    (echo-off)
    (read-line sb-sys:*tty*)
    (echo-on)))

但是,您可能需要确保在展开堆栈时始终将回声重置,并在输入内容之前清除输入:

However, you might want to ensure that the echo is always reset when unwinding the stack, and clear the input before inputting things:

(defun read-silently ()
  (echo-off)
  (unwind-protect 
      (progn
        (clear-input sb-sys:*tty*)
        (read-line sb-sys:*tty*))  
    (echo-on)))

CL-CHARMS

这里是使用libcurse的替代方法.以下内容足以进行简单的测试.

CL-CHARMS

Here is an alternative using libcurse. The following is sufficient to make a simple test work.

(defun read-silently ()
  (let (input)
    (charms:with-curses ()
      (charms:disable-echoing)
      (charms:enable-raw-input)
      (clear-input *terminal-io*)
      (setf input (read-line *terminal-io*))
      (charms:disable-raw-input)
      (charms:enable-echoing))
    input))

此外,使用libcurse可能有助于您实现美观的nice子手游戏机.

Besides, using libcurse might help you implement a nice-looking hangman console game.

这篇关于常见的Lisp,如何掩盖键盘输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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