473,804 Members | 3,138 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5766
In article <91************ *************** *******@e6g2000 prf.googlegroup s.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.progr ammer 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
11026
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 directory. It had TERMIOS.py but no termios.py. Could any body explain to me where can I found this file and solve this problem? Thanks in advance. Henry
3
7112
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 now getting reports of "your fonts are too tiny" from various users. Of course, the sites still look fine if those users set their text-size to medium, but face it -- most people don't even know thats an option.
9
2534
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. The typist was often too fast and the print bars would jam. I heard the same story when I took a typing class. This led me to wonder if 't' might have been moved from the home row for the same reason. 'E is dit and T is dash, under middle...
23
7756
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 buffer ? are they same ? thanks ^^
6
2817
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 statement and I have to press "enter" multiple times (maximum 4, it is not consistent though). I am trying it on a hp-ux machine with aCC compiler. Any pointers/help would be greatly appreciated.
0
1624
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. On my system (Mac OS 10.3.7) I specifically have ncurses. The programs I'm running turn off echoing and set raw mode but don't disable interrupts. For development purposes I like having interrupts, but my preferred keystrokes (WordStar) conflict...
0
2349
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 program calls the pg_restore command with the aid of the wonderful pexpect module. Everything works fine if I start this program from a tty. When I try to start the same program from cron, I get this nasty exception: Traceback (most recent call...
3
4734
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 <stdio.h> #Iinclude <termios.h> int main (void) {
1
1738
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( myFd, myStr, myLen ) It is placing myStr into an output buffer, implemented in the driver, that will send out all bytes through the UART.
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10077
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9152
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5522
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.