Getting keyboard "key events"

Q&A's, tips, howto's

Getting keyboard "key events"

Postby Stefan » Sun Jun 22, 2014 6:51 pm

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))
Stefan
 
Posts: 10
Joined: Fri Jun 21, 2013 7:01 am
Location: Germany

Re: Getting keyboard "key events"

Postby ralph.ronnquist » Mon Jun 23, 2014 5:26 am

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
ralph.ronnquist
 
Posts: 214
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

Re: Getting keyboard "key events"

Postby Stefan » Mon Jun 23, 2014 7:40 pm

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.
Stefan
 
Posts: 10
Joined: Fri Jun 21, 2013 7:01 am
Location: Germany


Return to newLISP in the real world

Who is online

Users browsing this forum: No registered users and 1 guest

cron