LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-04-2004, 09:24 AM   #1
BooKA.
LQ Newbie
 
Registered: Nov 2003
Posts: 9

Rep: Reputation: 0
Question Detecting Arrow Key Presses in C / C++


Hi,
I am currently writing a program with a buffered input, when you hit return the last command is stored in a linked list and you can scroll up and down through your history.

A duplicate of the shell history in linux. Unfortunately I am unable to get my program to recognise a key press by the up and down arrows.

I have tried a few things, no success so far, was wondering if anyone had any suggestions?!

Thanks
 
Old 04-04-2004, 10:20 AM   #2
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
Hi BooKA!!!

Well, working with arrow keys is difficult. That's why some editors, as vi(m), don't use (it can use, but not recommended) arrow keys, because arrow keys use a sequence of ASCII codes and it's hardware related. At the machine I'm now, the up arrow key sends <Esc>-O-A in sequence.

The way to do it with other keys is using getchar from stdin... Back on Windows days, I was used to use the header conio.h and the function getch() to track user's keys.

To sum up, there's no ASCII code for arrow keys, because ASCII comes from teletypes ages, where there was no arrow keys

What you could do is to use a third part library like curses (function wgetch() I think) to use arrow keys.

Good luck!

Last edited by Mega Man X; 04-04-2004 at 12:18 PM.
 
Old 04-04-2004, 10:24 AM   #3
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
Oh yeah, currently I'm trying to learn how to use pygame, and there's a function called pygame.event.Event() that you can keep track of all keys. Most likely, SDL must have a similar function, since pygame is basically a SDL wrap. So I really believe that the only way of using arrow keys is with another library, as SDL, curses, pygame...

Regards!
 
Old 04-04-2004, 04:26 PM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
You could also use the readline lib. It does all the history stuff for you. IIRC bash itself uses it.
 
Old 04-04-2004, 04:57 PM   #5
krajzega
Member
 
Registered: Jan 2004
Location: Poland
Distribution: FreeBSD 5.1
Posts: 92

Rep: Reputation: 15
Hmm...you can use ncurses library:
#include <ncurses.h>

...
window *win;
initscr(); //remember about errors support
win=newwin(0,0,10,10);
keypad(win,1); //sets "using-keypad" to true
key=getch();

Arrow key's code is defined as:
KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT

Good luck
 
Old 04-05-2004, 07:58 AM   #6
BooKA.
LQ Newbie
 
Registered: Nov 2003
Posts: 9

Original Poster
Rep: Reputation: 0
I have a solution using <ncurses.h> but I can't use it as this is for a project and it would mean using ncurses for the whole thing

Is there a way not using ncurses

Quote:
#include<ncurses.h>

int main()
{
int ch;

/* Curses Initialisations */
initscr();
raw();
keypad(stdscr, TRUE);
noecho();

printw("Welcome - Press # to Exit\n");

while((ch = getch()) != '#')
{
switch(ch)
{
case KEY_UP: printw("\nUp Arrow");
break;
case KEY_DOWN: printw("\nDown Arrow");
break;
case KEY_LEFT: printw("\nLeft Arrow");
break;
case KEY_RIGHT: printw("\nRight Arrow");
break;
default:
{
printw("\nThe pressed key is ");
attron(A_BOLD);
printw("%c", ch);
attroff(A_BOLD);
}
}
}

printw("\n\nBye Now!\n");

refresh();
getch();
endwin();

return 0;
}
Thanks
 
Old 04-07-2004, 01:18 AM   #7
korsuas
LQ Newbie
 
Registered: Apr 2004
Posts: 1

Rep: Reputation: 0
well, don't have a c compiler available right now, but I'll try to put it as correctly as I can.

you need the scancodes for VK_UP, VK_DOWNm etc.

I can tell you the they are about 72, 85, 70 smtg.

anyhowm to find them just

while (c=getch()!='\r') //enter or whatever
{
printf ("The char %c has the ASCII code %d\n", c, c);
}

Now, you will easily get the scancodes for all the keys.

One thing though, if you don't program right, you may have a duplicate for - I cannot remember right - lets say VK_UP the same as CTRL+M.

you have to:


c=getch();
if (c == 0)
{c = getch();
//you have an extended key - up, down, left, right
switch (c)
{
case 72: //code for up //probably
}
}
else
{
//code for normal chars like a, A, b, B, etc
}

Hope this help,
Regards,

Adrian Korsuas
korsuas@yahoo.com
 
Old 04-07-2004, 02:51 AM   #8
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
Here, I just did it in Free Borland:

Code:
#include<iostream>
#include<conio.h>
using namespace std;

int main(){
    int key;                 // A simple integer to keep track of ascii code
    system("cls");
  
    while (key != 27){  // ASCII for Esc key
	key = getch();
        cout << "The key you've pressed is:" << key << endl;
        system("cls");  // clean the screen
    }

    return 0;
}
My results are:

Up key = 72
Left key = 75
Right key = 77
Down key = 80

So, as you can see, my keys returns a different ASCII code then korsuas, so what I said before stands, arrow keys are hardware related (I'm running a swedish 105 keyboard). The only way to grab the keys correctly is with a third part library or using ordinary keys (a, b , c, numbers, etc).

Also note that A key returns a different value than a key (65 and 97 respectively), so when using key grabs on your program, make sure to use functions to Lower or Upper case to make it accurate. You can always check ASCII code here:

http://www.asciitable.com/

Last edited by Mega Man X; 04-07-2004 at 03:27 AM.
 
Old 04-07-2004, 06:10 AM   #9
BooKA.
LQ Newbie
 
Registered: Nov 2003
Posts: 9

Original Poster
Rep: Reputation: 0
Thanks Guys,

Will take your advice and check out an alternative set of keys to use.

If I get any improvements I will let you know.



Thanks Again!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Simulating Key Presses Hal Programming 2 09-02-2005 05:05 PM
Tab Key and arrow key zillah Solaris / OpenSolaris 12 05-26-2005 02:51 PM
Arrow Key mapping lazlom AIX 5 06-21-2004 03:32 PM
down arrow key problem rcbob Linux - Software 0 06-05-2004 07:58 PM
How to listen to all key presses in whole Linux? pvv Programming 1 03-28-2004 01:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration