Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
checking status of shift key [SOLVED]
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
mixmasta
Tux's lil' helper
Tux's lil' helper


Joined: 16 Oct 2004
Posts: 80
Location: Earth, Sol, Orion Arm, Milky Way Galaxy, Virgo Super-Cluster

PostPosted: Sat May 21, 2005 11:15 pm    Post subject: checking status of shift key [SOLVED] Reply with quote

Anyone know how to check the status of the shift key in a script? I've looked all over online but can't find a good solution to this seemingly simple question. I have to pay for internet by the hour and am not making good progress so far.

In the linux console I'd like to run an init script and if I am holding down, say the shift key, the script will take a different direction than if I hadn't held down the key while it ran. That is, the program should read the key status and not block waiting for a keypress.

I'd prefer the solution be in C, python, or maybe a existing binary to be called with bash. Sorry if this is a dumb question. I saw some functions that fit the bill but they were in things like svgalib and game libraries, and I thought there might be a simpler solution that a C guru would know.

Thanks in advance if you can help...


Last edited by mixmasta on Tue May 31, 2005 10:33 pm; edited 1 time in total
Back to top
View user's profile Send private message
StringCheesian
l33t
l33t


Joined: 21 Oct 2003
Posts: 887

PostPosted: Sun May 22, 2005 11:47 pm    Post subject: Reply with quote

I know this would be overkill, but it the only solution I can think of. Game libraries like SDL and Allegro would make that easy.

BTW, it may not be possible to check if the shift key is down from within an xterm. Your program might need to run without X running (SDL can be compiled with framebuffer console support), or alternatively it would need to open an X window (again, SDL would be useful). I'm just guessing - I don't know that much about X.
Back to top
View user's profile Send private message
canal
Apprentice
Apprentice


Joined: 28 Sep 2004
Posts: 203

PostPosted: Mon May 23, 2005 12:30 am    Post subject: Re: checking status of shift key Reply with quote

Gosh. Have you read what he's asking about ? He does not want to grab keyboard for it's own program - like X, SDL and Co do. He just want to check while running script if Shift is depressed or not. To toggle interactive mode or something. Pretty obvious idea. Just one problem: there are no sane way to do this. Shift state is not reported to programs - keyboard is shared resource, after all. You can grab keyboard totally (like X server and or SDL do) - but then you'll start from "clean state" and will be forced to ask user to release shift and depress it again. And at that stage it's easier to just ask to press "i" - like RedHat does.

mixmasta wrote:
Anyone know how to check the status of the shift key in a script? I've looked all over online but can't find a good solution to this seemingly simple question. I have to pay for internet by the hour and am not making good progress so far.

And for good reason: you can not do it. Period.

This was short answer. Now for long answer. Kernel does not export this info. Best place to start will be from drivers/char/vt_ioctl.c and/or drivers/char/keyboard.c - I'm not really sure. And when you'll add custom ioctl you'll be able to write userspace profram to read shift state "on the fly". May be someone did this already. But any solution without kernel patch will force user to press shift after program is started - not what you really want, I presume.
Back to top
View user's profile Send private message
mixmasta
Tux's lil' helper
Tux's lil' helper


Joined: 16 Oct 2004
Posts: 80
Location: Earth, Sol, Orion Arm, Milky Way Galaxy, Virgo Super-Cluster

PostPosted: Mon May 23, 2005 2:05 am    Post subject: checking status of shift key Reply with quote

gulp! well, this might be getting over my head at this point.

I'm just looking for a way to defeat gdm from starting when I don't want it to, say when I'm troubleshooting something. Seems so easy as they do it from Windows, holding down the shift key to keep things from starting, or starting in a limited mode.

I supposed hacking the keyboard driver in the kernel would be cool, but it will take me maybe months of study just to save a few seconds a week! ;) I guess I'll put in on my list of projects.

I suppose I'll just make some custom grub entries in the meantime. Thanks for your help.
Back to top
View user's profile Send private message
dma
Guru
Guru


Joined: 31 Jan 2003
Posts: 437
Location: Charlotte, NC, USA

PostPosted: Tue May 31, 2005 2:59 am    Post subject: Re: checking status of shift key Reply with quote

mixmasta wrote:
Anyone know how to check the status of the shift key in a script? I've looked all over online but can't find a good solution to this seemingly simple question. I have to pay for internet by the hour and am not making good progress so far.

In the linux console I'd like to run an init script and if I am holding down, say the shift key, the script will take a different direction than if I hadn't held down the key while it ran. That is, the program should read the key status and not block waiting for a keypress.

I'd prefer the solution be in C, python, or maybe a existing binary to be called with bash. Sorry if this is a dumb question. I saw some functions that fit the bill but they were in things like svgalib and game libraries, and I thought there might be a simpler solution that a C guru would know.

Thanks in advance if you can help...


WARNING: THIS CODE SUCKS.

Code:
#include <sys/ioctl.h>
#include <asm/ioctls.h>
#include <stdio.h>
#include <fcntl.h>

int main(int argc, char *argv[]) {
    int fd;
    char shift_state;

    fd = open("/dev/console", O_RDWR);

    /*
     * From console_ioctl:
     *        TIOCLINUX, subcode=6
     *               argp  points  to  a char which is set to the value of the kernel
     *               variable shift_state.  (Since 1.1.32.)
     */

    shift_state = 6;
    ioctl(fd, TIOCLINUX, &shift_state);

    printf("shift_state = %i\n", (int)shift_state);

    close(fd);

    return shift_state;
}
Back to top
View user's profile Send private message
mixmasta
Tux's lil' helper
Tux's lil' helper


Joined: 16 Oct 2004
Posts: 80
Location: Earth, Sol, Orion Arm, Milky Way Galaxy, Virgo Super-Cluster

PostPosted: Tue May 31, 2005 9:09 pm    Post subject: Reply with quote

Wow!! Not only did you answer the question, you wrote the program. Amazing.

I nominate you for a promotion from Guru to God-like-dude or whatever is a level up or two.

Thanks!!!
Mike
Back to top
View user's profile Send private message
mixmasta
Tux's lil' helper
Tux's lil' helper


Joined: 16 Oct 2004
Posts: 80
Location: Earth, Sol, Orion Arm, Milky Way Galaxy, Virgo Super-Cluster

PostPosted: Tue May 31, 2005 9:10 pm    Post subject: Reply with quote

p.s. It works!!

Why does the code suck? I don't think its long enough to be able to suck. ;)
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum