LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to detect key codes of home/end/pageup/pagedwn/de/ insert keys (https://www.linuxquestions.org/questions/programming-9/how-to-detect-key-codes-of-home-end-pageup-pagedwn-de-insert-keys-745498/)

nathan 08-06-2009 02:48 AM

how to detect key codes of home/end/pageup/pagedwn/de/ insert keys
 
Hi,

I am using usb HID keypad ( mini keypad)
If i enable numlock i am able to scan digits 0 to 9,
but how can i detect key scan codes of pageup/page down/ left arrow right arrow /end/home/
when i am trying to display with scanf func, its displaying like this

home key: ^[[1~
uparrow : ^[[A
left arrow : ^[[D
right arrow : ^[[C

end key: ^[[4~
del key: ^[[3~
insert key: ^[[2~

how to read ascii values of these keys in C language

Thanks in advance

nathan

Hko 08-07-2009 01:43 AM

You can use the ncurses library to do that. See "man getch" for specific info on using Function, arrow, home, del, pg-down... keys.

Here's an example program reading keys from a book: http://books.google.nl/books?id=7q0T...ypad.c&f=false

Don't forget to compile with the -lncurses

nathan 08-10-2009 04:23 AM

errors in while compiling
 
Quote:

Originally Posted by Hko (Post 3634442)
You can use the ncurses library to do that. See "man getch" for specific info on using Function, arrow, home, del, pg-down... keys.

Here's an example program reading keys from a book: http://books.google.nl/books?id=7q0T...ypad.c&f=false

Don't forget to compile with the -lncurses

Hi,

Thanks for your reply,

I went through the link and copied sample program and trying to compile,
it is throwing fallowing errors.

compilation

gcc kp.c -o kp -lncurses
and the errors are

kp.c:31: error: stray ‘\342’ in program
kp.c:31: error: stray ‘\200’ in program
kp.c:31: error: stray ‘\235’ in program
kp.c:31: error: stray ‘\342’ in program
kp.c:31: error: stray ‘\200’ in program
kp.c:31: error: stray ‘\234’ in program
kp.c:31: error: stray ‘\342’ in program
kp.c:31: error: stray ‘\200’ in program
kp.c:31: error: stray ‘\235’ in program
kp.c:32: error: stray ‘\342’ in program
kp.c:32: error: stray ‘\200’ in program
kp.c:32: error: stray ‘\234’ in program
kp.c:32: error: expected expression before ‘%’ token
kp.c:32: error: stray ‘\342’ in program
kp.c:32: error: stray ‘\200’ in program
kp.c:32: error: stray ‘\235’ in program
kp.c:32: error: stray ‘\342’ in program
kp.c:32: error: stray ‘\200’ in program
kp.c:32: error: stray ‘\234’ in program
kp.c:32: error: stray ‘\342’ in program
kp.c:32: error: stray ‘\200’ in program
kp.c:32: error: stray ‘\235’ in program
kp.c:33: error: stray ‘\342’ in program
kp.c:33: error: stray ‘\200’ in program
kp.c:33: error: stray ‘\234’ in program
kp.c:33: error: ‘Unmatched’ undeclared (first use in this function)
kp.c:33: error: expected expression before ‘%’ token
kp.c:33: error: stray ‘\342’ in program
kp.c:33: error: stray ‘\200’ in program
kp.c:33: error: stray ‘\235’ in program
..................................................

code is:

#include <unistd.h>
#include <stdlib.h>
#include <curses.h>
#define LOCAL_ESCAPE_KEY 27
int main()
{
int key;
initscr();
crmode();
keypad(stdscr, TRUE);
noecho();
clear();
mvprintw(5, 5, “Key pad demonstration. Press ‘q’ to quit”);
move(7, 5);
refresh();
key = getch();
while(key != ERR && key != ‘q’) {
move(7, 5);
clrtoeol();
if ((key >= ‘A’ && key <= ‘Z’) ||
(key >= ‘a’ && key <= ‘z’)) {
printw(“Key was %c”, (char)key);
}
else {
switch(key) {
case LOCAL_ESCAPE_KEY: printw(“%s”, “Escape key”); break;
case KEY_END: printw(“%s”, “END key”); break;
case KEY_BEG: printw(“%s”, “BEGINNING key”); break;
case KEY_RIGHT: printw(“%s”, “RIGHT key”); break;
case KEY_LEFT: printw(“%s”, “LEFT key”); break;
case KEY_UP: printw(“%s”, “UP key”); break;
case KEY_DOWN: printw(“%s”, “DOWN key”); break;
default: printw(“Unmatched - %d”, key); break;
} /* switch */
} /* else */
refresh();
key = getch();
} /* while */
endwin();
exit(EXIT_SUCCESS);
}


kindly suggest me, where i went wrong ??

Thanks in advance

Nathan

catkin 08-10-2009 04:37 AM

Looks like a "smart quotes" problem. Try replacing the smart " and ' (that are different each side of the string they enclose) with ordinary ones.

Hko 08-10-2009 04:41 AM

You used the wrong single and double quotes characters.
IMO you should get familiar with your editor and the C programming language.

nathan 08-10-2009 06:04 AM

Quote:

Originally Posted by Hko (Post 3637535)
You used the wrong single and double quotes characters.
IMO you should get familiar with your editor and the C programming language.

Thank you very much for your reply..

now i want to run it on my target board..

for this i am compiling like this
arm-linux-gcc -s -static kp.c -o kp -lncurses
./kp

it is showing following error

Error opening terminal: vt102.

what changes i need to do?

Thanks in advance

Nathan

GrapefruiTgirl 08-10-2009 07:13 AM

NOTE: one or more participants in this thread seem to be clicking REPORT when they intend to click QUOTE.

Please observe that the first button on the lower right corner is for reporting offensive or inappropriate content (and other things) while the second button is for quoting another post.

Thanks, carry on!
Sasha


All times are GMT -5. The time now is 08:12 PM.