LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Raw access to keycodes/scancodes from userspace? (https://www.linuxquestions.org/questions/programming-9/raw-access-to-keycodes-scancodes-from-userspace-828886/)

hda7 08-27-2010 03:32 PM

Raw access to keycodes/scancodes from userspace?
 
Is there an interface whereby a userspace program can retrieve the raw keycodes/scancodes from a keyboard? I'm trying to make something in the way of a piano keyboard, and I need to know when keys are pressed (possibly simultaneously), and when they are released.

Aquarius_Girl 08-28-2010 08:07 AM

Quote:

Originally Posted by hda7 (Post 4079989)
Is there an interface whereby a userspace program can retrieve the raw keycodes/scancodes from a keyboard?

Have a look here:
http://www.linuxquestions.org/questi...1/#post3956458
and here too:
http://www.faqs.org/docs/Linux-HOWTO...-HOWTO.html#s1

hda7 08-28-2010 09:33 AM

Quote:

Originally Posted by anishakaul (Post 4080606)

The first link just appears to be getting raw character codes from the keyboard. I need the keycodes instead because I need to know when a key is released as well as when it is pressed. The second link mentions scancode and keycode modes, but doesn't mention how to switch modes. :( I looked at termios, but I can't find anything there either. Is there something I missed?

Aquarius_Girl 08-28-2010 09:45 AM

Quote:

Originally Posted by hda7 (Post 4080679)
but doesn't mention how to switch modes.

Can you explain this in more detail ?

hda7 08-29-2010 01:24 PM

Quote:

Originally Posted by anishakaul (Post 4080686)
Can you explain this in more detail ?

Well, The article in the second link mentions that when an application is in "keycode mode", keycodes are delivered to it instead of character codes. It does not mention how you would put your application in "keycode mode." Is there some ioctl or something you can call on stdin to do this?

Aquarius_Girl 08-30-2010 12:06 AM

Quote:

Originally Posted by hda7 (Post 4081599)
It does not mention how you would put your application in "keycode mode."

How about this ?
http://linux.die.net/man/1/kbd_mode



Check out the following links too (but after the first one):
http://gunnarwrobel.de/wiki/Linux-an...oard.html#sec5
and
http://gunnarwrobel.de/wiki/Linux-an...oard.html#sec5

linux_hy 08-31-2010 12:26 AM

you could
keyboard()
{
fd=open(/dev/null);
read(fd)
}

hda7 08-31-2010 07:35 AM

Quote:

Originally Posted by anishakaul (Post 4081946)

That looks like what I'm trying to do, except that:
* It is an external program (I would like a library function or system call I can call from within a program), and
* As far as I can tell, Its action would be global (if I suspend my program, I'd like to be able to type correctly).
Is there a library function or system call that replaces this functionality?

hda7 08-31-2010 07:36 AM

Quote:

Originally Posted by linux_hy (Post 4083128)
you could
keyboard()
{
fd=open(/dev/null);
read(fd)
}

Would that DO anything?

Aquarius_Girl 08-31-2010 08:03 AM

Quote:

Originally Posted by hda7 (Post 4083465)
Is there a library function or system call that replaces this functionality?

You can use system() function to call "kbd_mode" !

Note:
You should try searching Google before asking questions and then tell exactly what you found and how that was not helpful !

hda7 08-31-2010 09:14 PM

Quote:

Originally Posted by anishakaul (Post 4083497)
You can use system() function to call "kbd_mode" !

Note:
You should try searching Google before asking questions and then tell exactly what you found and how that was not helpful !

I know I could call kbd_mode. My main concern was that kbd_mode appears to affect all keyboard input. Is this correct?
I stay in console mode, switching back and forth between virtual terminals, and I don't want ALL my keyboard input to be in keycode mode.

Also, I just checked, and I don't have kbd_mode. :( If there's no library function to replace that functionality, is there somewhere I can get kbd_mode?

Aquarius_Girl 08-31-2010 09:21 PM

Quote:

Originally Posted by hda7 (Post 4084241)
Also, I just checked, and I don't have kbd_mode. :(

When you say you don't have kbd_mode does it mean when you run it from terminal nothing happens ??

Name your distribution !

linux_hy 09-01-2010 02:02 AM

Quote:

Originally Posted by hda7 (Post 4083467)
Would that DO anything?

sorry I take a mistake,it should be open("/dev/input/event")

I mean that to open a device file of keyboard with the system call "open"
and read data from the file,when a key is pressed or released the event have been save into the struct input_event
this is defined in /usr/include/linux/input.h. else some key code and input event defined in it
the define is following
struct input_event
{
struct timeval time;
_u16 type;
_u16 code;
_u32 value;
};
a simple example:
struct input_event ie;
memset(&ie,0,sizeof(struct input_event));
int fd=open("/dev/input/event0",O_RDONLY);
read(fd,&ie,sizeof(struct input_event))
switch(ie.code)
{
case KEY_ESC:
exit(0);
break;
case KEY_F1:
.....//your code
break;
........
default:
printf("No key pressed\n");
}
the above code may be take mistake too
:-)

hda7 09-01-2010 01:13 PM

Quote:

Originally Posted by anishakaul (Post 4084249)
When you say you don't have kbd_mode does it mean when you run it from terminal nothing happens ??

Name your distribution !

I mean, when I say "which kbd_mode", it returns command not found. My distro is Puppy Linux 4.00, and is listed on the sidebar of my posts.

Aquarius_Girl 09-02-2010 08:18 AM

Quote:

Originally Posted by hda7 (Post 4085122)
I mean, when I say "which kbd_mode", it returns command not found. My distro is Puppy Linux 4.00, and is listed on the sidebar of my posts.

I tried :
Code:

anisha@linux-dpjj:~> which kbd_mode
/bin/kbd_mode

on OpenSuse 11.2.

Check which package/RPM/DEB/tar etc is needed for Puppy Linux for keyboard programming !


All times are GMT -5. The time now is 08:42 AM.