Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detect keyboard butons

Status
Not open for further replies.

Leibnitz

Programmer
Apr 6, 2001
393
0
0
CA
I'm writing a program that detects which buton on the keyboard that the user have hit.How would i detect butons that are the combinaison of two characters( ex: delete, pageUp, pageDown, etc ).
 
What OS are you using? Some OSs like Windows have routines to do that. There are two ways.

1) Using getch
2) Using the keyboard matrix

With getch, you have to decode the sequences. For instance, the uparrow key might produce ESC A. All you need to do is work out what sequence they produce. The problem then is what happens if the user presses ESC today and A tommorow. Since your program is waiting on the keyboard, it cannot tell the difference between ESC A being a keyboard sequence or a single uparrow key.

Decoding the keyboard matrix is OS specific. In DOS, there was an INT call to tell you which row and which column had been pressed. You could detect whether the user pressed left shift or right shift: something you can't even begin to do with getch.
 
I'm using windows xp.I think that i'm going to use the "getch() method" because it looks more simple.Can you please post a code sample that does what you just said about the escape sequences so it would help me to get started.

thanks !
 
Try this:

Code:
char c;

c = getch();
if (c == 0)
  c = getch();

The special keys are producing extended key-codes, 2 bytes long, of which the first is usually 0... You can gather the values you need easily then...
[red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
Ok,i have tried it but it does'n seems to work.
I have use this code:
Code:
#include <conio.h>

void main()
{
    char c;

	c = getch();

	if (c == 0)
	{
		c = getch();
		putch(c);
	}
}


 
err... try getchar()? [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
I have tried it but there is still no results.
 
hmmm... weird... I am using XP too and it is the first time that I encounter this problem.
getchar expects an eol character to complete, so going back to getch() would be good.

You can see though that the first character returned is the same, for each of the keys you specify (224, here or -32).

The interesting thing is that for functional keys, the first getch() returns 0, as I was expecting it...
[red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
****************************************************************
Ok i've got it.
Code:
#include <conio.h>
#define CTRL_Z 26
#define FRONT_ARROW 77
#define BACK_ARROW 75
#define DOWN_ARROW 80
#define UP_ARROW 72


void main()
{
	int ch;

    while(( ch = getch() ) != CTRL_Z ) // stops with CTRL-Z
    {
        switch(ch) {
		case FRONT_ARROW:
			cprintf(&quot;Front arrow.\r\n&quot;);
			break;
		case BACK_ARROW:
			cprintf(&quot;Back arrow.\r\n&quot;);
			break;
		case DOWN_ARROW:
			cprintf(&quot;Down arrow.\r\n&quot;);
			break;
		case UP_ARROW:
			cprintf(&quot;Up arrow.\r\n&quot;);
			break;
		}//etc
	}
}

Thanks for your help.
 
You could also try this one
Code:
#include <stdio.h>
#include <conio.h>

int main(int argc, char* argv[])
{
    int ch;

    printf (&quot;Press q to quit\n&quot;);
    do {
        ch = _getch ();
        if (ch == 0xE0 || ch == 0)
        {
            int second = _getch ();
            printf (&quot;%02x %02x\n&quot;, ch, second);
        }
        else
        {
            printf (&quot;%02x\n&quot;, ch);
        }
    } while (ch != 'q');

    return 0;
}
 
****************************************************************
good one ! I have also wrote this before.

Code:
#include <conio.h>
#define CTRL_Z 26

void main()
{
    int ch;
    while(( ch = getch() ) != CTRL_Z ) // stops with CTRL-Z
    {
        cprintf(&quot; character = %c   ascii code = %d&quot;, ch, ch );
        cprintf(&quot;\r\n&quot;);
    }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top