Getting keyboard "key events"

Q&A's, tips, howto's
Locked
Stefan
Posts: 10
Joined: Fri Jun 21, 2013 7:01 am
Location: Germany

Getting keyboard "key events"

Post by 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))

ralph.ronnquist
Posts: 228
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

Re: Getting keyboard "key events"

Post by ralph.ronnquist »

Not sure how much effort it takes, but I'd check out using "curses".
Done before:
viewtopic.php?f=12&t=240&hilit=curses

Stefan
Posts: 10
Joined: Fri Jun 21, 2013 7:01 am
Location: Germany

Re: Getting keyboard "key events"

Post by Stefan »

Thanks! It works with ncurses:

Code: Select all

;;;
;;; 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.

Locked