How can I get the keyboard state in Linux?

Viewed 21821

I want to check if the user pressed down the Shift key when the program starts. (That means, press down the Shift key before the program is started) It's a simple console program, nothing related to X.

This maybe similar to the Win32 GetKeyboardState() function.

I want to know whether I can do this and how, but not any pros and cons with accessing the terminal directly.

4 Answers

I have found a very simple way through gtk/gdk.

int main ( int argc, char *argv[], char *env[] )
{
    gtk_init(&argc, &argv);

    GdkModifierType button_state;
    gdk_window_get_pointer(NULL, NULL, NULL, &button_state);
    if(button_state & GDK_CONTROL_MASK) {
        printf("ctrl key is pressed");
    }
}
Related