ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?!
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.
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...
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
}
#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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.