0

okay so i have a program i am writing , and basically i am going to be taking input for keyboard keys such as left arrow, right arrow, up and down etc and my question is , in what is the best option to scan in these keys so that i can make my program run both in linux and windows

and what am i scanning exactly? am i supposed to scan the ascii values and store them in int? chars? or is it another way to do this ? i have searched the internet and i am finding that the kex values for keyboard scan codes are e0 4b e0 4d e0 48 e0 50

but when i actually scan the values using getchar() and store them into ints i get 4 values for each key pressed namely for example 27 91 67 10 , 27 91 68 10

i understand that each key has press release and other values attached to it , so should i be scanning for the 67 68 etc range?

or is there another way to do this

i am writing the program using c language

thingybingytie
  • 170
  • 1
  • 15
  • possible duplicate of [Curses getting arrow keys](http://stackoverflow.com/questions/1182665/curses-getting-arrow-keys) – David C. Rankin Feb 27 '15 at 15:45
  • You are confusing scan code with char code. – user4098326 Feb 27 '15 at 15:45
  • It depends if you are coding a terminal application (then I would suggest using a library like [ncurses](https://www.gnu.org/software/ncurses/ncurses.html) or a graphical application (then use a toolkit like [Qt](http://qt-project.org/) ....) – Basile Starynkevitch Feb 27 '15 at 16:49
  • does ncurses also provide the api to create windows and apply images or videos to them? – thingybingytie Mar 01 '15 at 02:26
  • also does SDL2 and ncurses do the same thing? because i was learning this api recently and it does have keyboard events, however i am curious to know which is better so that i can port my program over to both linux and windows – thingybingytie Mar 01 '15 at 02:31

2 Answers2

1

In Linux, it seems like you're seeing ANSI escape sequences. They are used by text terminals, and start with the Escape character, which is '\x1b' (decimal 27).

This is probably not what you want, if you want to make something keyboard-controllable in direct, game-like manner you need to use "raw" input. There's plenty of references for that, look at ncurses for instance.

unwind
  • 391,730
  • 64
  • 469
  • 606
0

Open a terminal and use the command xev. You can then press any key you want and see its corresponding codes. You can also move and click the mouse to see what happens there.

Richard
  • 56,349
  • 34
  • 180
  • 251