0

My Problem is the following:

I want to make a program, a simple text-editor, which can work with all keys of a keyboards set such as the Function Keys (F1 to F12), Command, Left-Shift, ALT or TAB.

More precise, I want to be able to assign all keys to specific operations.

I have made a program to print out the ASCII values of those keys, but the keys doesn´t get accepted as i want to, my test program prints out either values of several ASCII codes or doesn´t accept it all like by pressing the Command Key, instead:

My test-program to print out the ASCII-value of a pressed key:

#include <stdio.h> 

int main (void)
{
    char k;

    printf("\n\n\n\n");

    while(1)
    {

       printf("Please press any key:");

       k = getchar();
       getchar();

       printf("ASCII code of given input key: %d",k);
       printf("\n\n\n");
    }
}

Output after pressing on F2-Key:

Please press any key:^[OQ
ASCII code of given input key: 27


Please press any key:ASCII code of given input key: 81

Output after pressing on F2-Key:

Please press any key:^[[15~
ASCII code of given input key: 27


Please press any key:ASCII code of given input key: 49


Please press any key:ASCII code of given input key: 126

Output after pressing on Command-,Alt- or Left-Shift-Key:

Please press any key:               // no reaction at all

Due to the fact, that i think this must be done easily, because of course its normal to use all keys of a keyboard within a program, it isn´t quite so easy for me as a newbie to find the proper way to achieve that.

I have found that question here on Stackoverflow but it belongs to C#: Special keys on keyboards

Why i can´t fetch an ASCII value of those keys?

Sorry, if i might have an mistaken impression that these keys belong to ASCII values.

  • 1
    *"Why i can't fetch an ASCII value of those keys?"* -- because those keys don't have an associated ASCII value. What you get when pressing `F2` is two bytes of data that have nothing to do with ASCII, and just happen to get read by your program because the terminal passes them to it. Other keys are just ignored and not passed to the program from the terminal. If you want to capture every keystroke possible you will need to [read the terminal in raw mode](https://viewsourcecode.org/snaptoken/kilo/02.enteringRawMode.html). – Marco Bonelli Oct 27 '19 at 15:42
  • There are *many* different keyboards - like [space cadet keyboard](https://en.m.wikipedia.org/wiki/Space-cadet_keyboard), [Sun type 7 keyboard](https://deskthority.net/wiki/Sun_Type_7) and even more weird ones.. do you want to handle *all* of them? – Jesper Juhl Oct 27 '19 at 15:59
  • @JesperJuhl Aren´t `CMD`, `ALT`, `TAB` and `Left-Shift` as so crucial keys anyhow standarized? – RobertS supports Monica Cellio Oct 27 '19 at 16:02
  • @RobertS Keyboards are anything *but* standardized. – Jesper Juhl Oct 27 '19 at 16:05
  • @JesperJuhl I mean the ASCII code is standarized. Why then not these crucial keys? How do you use those keys inside a program of yours? – RobertS supports Monica Cellio Oct 27 '19 at 16:06
  • 1
    @RobertS "How do you use those key inside a program of yours?" I rely on the operating system (or a toolkit like Qt or SFML) decoding them for me. Trying to just read raw keyboard codes without an OS driver or toolkit in between would be a serious waste of my time. – Jesper Juhl Oct 27 '19 at 16:10
  • @MarcoBonelli I can´t understand why this question is not so frequent. I mean, How programmers incorporate the use of these crucial keys into their programs? Not at all? – RobertS supports Monica Cellio Oct 27 '19 at 16:10
  • @RobertS what does my comment have to do with the question being frequent or not? It is very frequent; my only guess is that people that want to use the terminal in raw mode are usually more experienced and can manage to work it out by their own without the need to ask on here. – Marco Bonelli Oct 27 '19 at 16:12
  • @MarcoBonelli Assuming you want to program a text editor in C and use this one outside the terminal as standalone. How do you use these keys there? – RobertS supports Monica Cellio Oct 27 '19 at 16:14
  • @RobertS thats a whole different question, and the answer is much more complicated. – Marco Bonelli Oct 27 '19 at 16:19
  • @MarcoBonelli That is the answer i primarily looking for. What do you suggest to alter in my question? – RobertS supports Monica Cellio Oct 27 '19 at 16:23
  • @RobertS I'm afraid your question is not very suitable for Stack Overflow. You're looking for a guide or tutorial, which is off-topic here. You should search the web or ask on another site. – Marco Bonelli Oct 27 '19 at 16:24
  • 2
    The answer is that it is platform specific, and even on a single platform may depend on the type of application - A GUI application will receive keyboard _messages_, while a Windows console program may invoke a function that returns scan-codes, and a Linux terminal program may receive escape sequences or possibly scan-codes depending on what level and through which device you might choose to access the keyboard. So you question requires some context regarding the platform. It may be possible via a cross-platform library to use common code across platforms, but then you need to pick your lib. – Clifford Oct 27 '19 at 16:36
  • @Clifford This is much more complicated than i´d thought, but i´m happy that you guide me to some topics to learn more about to achieve the task i want. – RobertS supports Monica Cellio Oct 27 '19 at 16:40
  • 1
    If you're programming in Linux, a good place to start might be understanding how the OS handles things like this -- Ctrl+C, for example, fires a signal, that your code can handle. [Hre's more information on signals, and a demonstration of signal handling code.](https://www.thegeekstuff.com/2012/03/catch-signals-sample-c-code) – jhelphenstine Oct 27 '19 at 16:54
  • 2
    Your search-fu needs honing perhaps? For example: https://stackoverflow.com/questions/24708700/c-detect-when-user-presses-arrow-key. Note that the special key codes are _not_ ASCII codes. ASCII is a character encoding (with a limited set of control codes), not a keyboard encoding "CURSOR-LEFT" is not a character (and has no specific control code). – Clifford Oct 27 '19 at 17:10
  • @jhelphenstine yes, currently i´m under Linux Ubuntu as the course i have is build on, but want to go cross for building such program also under Windows in the future. – RobertS supports Monica Cellio Oct 27 '19 at 17:12

1 Answers1

1

Access to "special keys" is not part of the standard library because the language does not assume any specific I/O hardware such as a screen and keyboard, it only assumes standard character based I/O streams, and the data on these streams relate to characters, not keys.

As such receiving keyboard events and scan-codes and platform specific. However there are cross-platform mean by which you can get some commonality across systems that support a keyboard (such as desktop computers - not everything C runs on is a desktop computer).

For example for text console applications such as in your example you might use the ncurses library which is a clone of the Unix curses library, and has a Windows near equivalent in the PDcurses library.

In these curses variants for example, you can write:

int ch = getch() ;
if(ch == KEY_LEFT)
    printw("Left arrow is pressed\n");

Under the hood it is a wrapper for the platform specific means. If cross-platform is not an issue you may utilise these means directly. For example MSVC++ has a conio library, which has a _getch() function similar to that above, except that for non-character keys, it returns 0x00 or 0xE0, and returns teh key code when called a second time.

For GUI applications, the applications message loop will receive keyboard events, the exact means will vary between systems, but again there are cross-platform solutions such as Qt or WxWidgets for example.

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • yes, i thought about the ncurses-library furthermore to get an screen outside the terminal itself. But with regards to your given example. How do i achieve that the macro `KEY_LEFT` is matching to the output of the key of the left cursor? – RobertS supports Monica Cellio Oct 27 '19 at 17:22
  • 1
    `KEY_LEFT` is defined in the library header - https://pubs.opengroup.org/onlinepubs/7908799/xcurses/curses.h.html. See also http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/keys.html. Curses _generalises_ key codes, so for example it has `KEY_NPAGE` (next page) rather then "page-down". It is down to the implementation to map the available keys to these codes. – Clifford Oct 27 '19 at 18:05