Getting keyboard "key events"

Started by Stefan, June 22, 2014, 11:51:07 AM

Previous topic - Next topic

Stefan

For a simple "game" (console based) I need to get the keyboard "key events" from my Mac.

(read-key) seems to be a good choice, but unfortunately some keys (or modified keys: shift, ..) do require following (read-key)s.

Worse on Mac is that "cursor up" results in three (read-key)s returning: 27 91 65 and ESC in 27! So I can not distinguish between ESC and cursor keys.



Is there a better solution?



Stefan



ps From the newLISP docu: (while (!= (set 'c (read-key)) 1) (println c))

rrq

#1
Not sure how much effort it takes, but I'd check out using "curses".

Done before:

http://www.newlispfanclub.alh.net/forum/viewtopic.php?f=12&t=240&hilit=curses">//http://www.newlispfanclub.alh.net/forum/viewtopic.php?f=12&t=240&hilit=curses

Stefan

#2
Thanks! It works with ncurses:

;;;
;;; simplified newlisp ncurses example to get key codes
;;;

;;; import functions from ncurses lib
(set 'ncfuncs '( "initscr" "endwin" "getch" "cbreak" "keypad"))
(define (import-ncurses) (dolist (x ncfuncs ) (import "/usr/lib/libncurses.dylib" x)))

;;; Newlisp-Ncurses
(import-ncurses)
(set 'stdscr (initscr))
(println (format "%ld" stdscr))
(cbreak)
(keypad stdscr 1)
(set 'key (getch))
(endwin)
(println (string "key: " key))


This should be sufficient for my purpose.