473,240 Members | 1,845 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,240 software developers and data experts.

termios and keyboard medium raw mode

Hi

I wrote the following program that reads the keyboard in medium raw
mode (keycode mode). Here is the initialization code, the full code is
at the end.

fd = open("/dev/tty0", O_RDONLY);
tcgetattr ( fd, &newkbd );
newkbd.c_lflag &= ~ (ECHO | ICANON | ISIG);
newkbd.c_iflag = 0;
newkbd.c_cc[VMIN] = 18;
newkbd.c_cc[VTIME] = 1;
tcsetattr ( fd, TCSAFLUSH, &newkbd );
ioctl ( fd, KDSKBMODE, K_MEDIUMRAW );

It's suppose to control the value of linvel and rotvel (linear and
rotational velocity) when the user presses the arrow keys or the WASD
keys. To handle multi keys at the same time, I set the value when the
user presses the key, and reset it to 0 on release.

However it doesn't work. Let say I press UP, then RIGHT while
maintaining UP pressed, then release UP while maintaining RIGHT
pressed, then release RIGHT. In that case, I receive the following
events: UP pressed, RIGHT pressed, Right released. UP released is
missing. What am I doing wrong? Can you suggest another way of doing
this?

Also, the terminal keeps scrolling down, as if I was constantly
pressing ENTER. Why? Besides, I have to run this code as super user,
which is not really nice...

I am using linux, kernel 2.6.22 and gcc. Portability is not an issue.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <termios.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/kd.h>
#include <linux/keyboard.h>

int fd=-1, oldkbmode=K_RAW;
struct termios orig_kb;
float linvel=0, rotvel=0;

void clean_up ( void ) {
ioctl ( fd, KDSKBMODE, oldkbmode );
tcsetattr ( fd, 0, &orig_kb );
close ( fd );
}

void printVel(int i) {
printf("%f %f\n", linvel, rotvel);
alarm(1);
}

int main(){
int keycode, pressed, controlPressed=0;
unsigned char buf[18]; /* divisible by 3 */
int i,n;
struct termios newkbd;

/* Open and configure the keyboard */
fd = open("/dev/tty0", O_RDONLY);
tcgetattr ( fd, &orig_kb );
tcgetattr ( fd, &newkbd );
newkbd.c_lflag &= ~ (ECHO | ICANON | ISIG);
newkbd.c_iflag = 0;
newkbd.c_cc[VMIN] = 18;
newkbd.c_cc[VTIME] = 1;
tcsetattr ( fd, TCSAFLUSH, &newkbd );

/* Set medium raw mode: we receive keycodes. */
ioctl ( fd, KDGKBMODE, &oldkbmode );
ioctl ( fd, KDSKBMODE, K_MEDIUMRAW );

/* Restore the normal keyboard mode on exit */
atexit(clean_up);

/* Every 1 second, print the value of linvel and rotvel */
signal(SIGALRM, printVel);
alarm(1);

while( 1 ){
/* Wait until a key is pressed or released */
n = read ( fd, buf, sizeof ( buf ) );

/* Retrieve the key code and whether the key was pressed or
released */
i = 0;
while ( i < n ) {
pressed = (buf[i] & 0x80)==0x80 ? 0 : 1;
if ( i+2 < n && ( buf[i] & 0x7f ) == 0
&& ( buf[i+1] & 0x80 ) != 0 && ( buf[i+2] & 0x80 ) != 0 )
{
keycode = ( ( buf[i+1] & 0x7f ) << 7 ) | ( buf[i+2] & 0x7f );
i += 3;
}
else keycode = ( buf[i++] & 0x7f );
}

/* Take appropriate action */
switch ( keycode ) {
case 103: //UP ARROW
case 17: //W
linvel = pressed ? 1 : 0;
break;
case 108: //DOWN ARROW
case 31: //S
linvel = pressed ? -1 : 0;
break;
case 105: //LEFT ARROW
case 30: //A
rotvel = pressed ? -1 : 0;
break;
case 106: //RIGHT ARROW
case 32: //D
rotvel = pressed ? 1 : 0;
break;
case 29: //CTRL
controlPressed = pressed;
break;
case 46: //C
if( controlPressed && pressed ) exit(0);
break;
}
}
return 0;
}
Mar 13 '08 #1
2 5699
In article <91**********************************@e6g2000prf.g ooglegroups.com>,
Brice Rebsamen <br************@gmail.comwrote:
>Hi

I wrote the following program that reads the keyboard in medium raw
mode (keycode mode). Here is the initialization code, the full code is
at the end.

fd = open("/dev/tty0", O_RDONLY);
(With love and affection)

Off topic. Not portable. Cant discuss it here. Blah, blah, blah.

--
Useful clc-related links:

http://en.wikipedia.org/wiki/Aspergers
http://en.wikipedia.org/wiki/Clique
http://en.wikipedia.org/wiki/C_programming_language

Mar 13 '08 #2
Brice Rebsamen wrote:
Hi

I wrote the following program that reads the keyboard in medium raw
mode (keycode mode).
[Snip]
I am using linux, kernel 2.6.22 and gcc. Portability is not an issue.
I suggest you try a group appropriate to your platform, as this question
is about platform-specifics rather than the C language.

comp.unix.programmer would probably be a good start.
Mar 13 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: henry xie | last post by:
Hi, All: I am a newbie in Python. I am trying to use pexpect package in the project. But when I installed the package, it couldn't be used as termios.py was missing. I checked the LIB...
3
by: Marti | last post by:
Dear everyone, Its my understanding that IE6 now uses a default text-size of "Small" rather than IE5's "Medium". Since I have used relative font-sizes (usually in ems) on all my sites, I am...
9
by: skearney | last post by:
When I was in boy scouts, as part of learning Morse code, I was told that the inventor of the typewriter originally put the letter 'e' under the left middle finger, just below its present position....
23
by: herrcho | last post by:
What's the difference between STDIN and Keyboard buffer ? when i get char through scanf, i type in some characters and press enter, then, where do the characters go ? to STDIN or Keyboard...
6
by: hem | last post by:
Hi, I have the following small program which read password from user after echoing off. But the problem is, it is freezing for some time (not sure about the duration) before going to the next...
0
by: Derek Peschel | last post by:
Should I add an RFE to SourceForge too? I'd like a wide audience in case someone has enough experience to comment or is solving the same problem. I'm using the urwid library which uses curses. ...
0
by: Laszlo Nagy | last post by:
Hi All, I have a python program that downloads database backups from a remote server, and tries to replace the local database with the downloaded backup. The database is a PostgreSQL server and my...
3
by: goblin | last post by:
Hi all I'm struggeling to get my serial port settings changed using termios. I can change the baud rate fine, but none of the control flags respond to my changes. As an example: #include...
1
by: jorba101 | last post by:
I'm programming an ARM's UART that comes with a library implementing the standard unix termios interface. Regarding termios, I have following trouble: When I do following syscall: write(...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.