tags:

views:

111

answers:

5

I'd like to write an application in C which uses arrow-keys to navigate and F-keys for other functions, such as saving to a file, language selection, etc.

Propably the values depend on the platform, so how could I find out which values the keys have?

If they don't, or if you know them, I don't have to know how to find out;)

Edit: My platforms are Linux and M$ Windows. Therefore, I'm looking for a solution as portable as possible.

(Propably something like

#ifdef __unix__
   #define F1 'some number'
   /* ... */
   #define ARROW_UP 'some other number'
#elif __WIN32__ || MSDOS /*whatever*/
   #define F1 'something'
   /* ... */
   #define ARROW_UP 'something different'
#endif

)

+1  A: 

I think that depends on $TERM, but either way it's going to be a sequence of characters. I get this:

% read x; echo $x | od -c --
^[[15~
0000000  033   [   1   5   ~  \n                                        
0000006

That's my F5 key, and apologies for this being a *nix-centric answer, if that's not your platform.

Paul Beckingham
A: 

If you're on a Unix or Linux machine, simply enter cntl-V and then the key while in vim*. You'll see the code.

HTH

cheers,

Rob

Rob Wells
A: 

This handy site has lots of useful nuggets of info on the raw scancodes:

http://www.win.tue.nl/~aeb/linux/kbd/scancodes.html#toc1

Kev
+1  A: 

This problem is a lot messier than anyone would like. In brief, each of these keys sends a sequence of characters, and the details depend on which terminal emulator is being used. On Unix, you have a couple of choices:

  • Write your app to use the curses library and use its interface to the terminfo database.

  • Parse the the terminfo database yourself to find the sequences. Or to use an API, look at the sources for tput to see how this is done.

  • Use the command-line program tput to help discover the sequences. For example, to learn the sequence for F10, run tput kf10 | od -a. Keep in mind this changes from terminal to terminal, so you should run tput each time you run your app.

  • Write your application using one of the X libraries and get access to 'key symbols' rather than a sequence of characters. the XLookupKeysym function is a good place to get started; you'll find names of the symbols in /usr/include/X11/keysymdef.h. If you are going to connect over the network from non-X systems (e.g., Windows) this option is not so good.

I have no idea how to do any of this on Windows.

Norman Ramsey
A: 

Some of the keys generate 2 character key codes. One technique I have successfully used on the pc, is to get the 2 characters, and set the 7th bit on the second character, and return that character as the answer. It lets the rest of the program treat each keystroke as a single character.

The solution to your problem with depend on what keys you need to support. Hiding the translation behind a function of some sort, is generally a good idea.

EvilTeach