-2
\$\begingroup\$

I is trying to makey makey a program that will detect when certain keys are pushed. My only problem is that I dont know the nombers that corrospond to the shift key and control keys. I am writing this code for a game. Here are teh codes:

switch(event->keyval) {
    case ?:
        printf("Shift key was pressed");
    break;
    case ??:
        printf("Control key waas pressed");
    break;
}

What can replace ""?"" and ""??""

Also, if possible, my answer SHOULD be able to fit in a char type

\$\endgroup\$
  • 2
    \$\begingroup\$ What framework do you use? \$\endgroup\$ – Alexandre Vaillancourt Jul 13 at 21:44
  • \$\begingroup\$ @AlexandreVaillancourt define framework for me please \$\endgroup\$ – Julian Tiemann Jul 13 at 22:35
  • 1
    \$\begingroup\$ What library/framework/engine defines event->keyval? To my knowledge, in c, to get a character, you need getchar(), which is not what is used here. \$\endgroup\$ – Alexandre Vaillancourt Jul 13 at 22:44
1
\$\begingroup\$

To get state of the shift and control buttons in GTK, you need to apply bitwise AND to the event state.

if(event->state & GDK_SHIFT_MASK)
{
    printf("Shift key was pressed");
}
else if(event->state & GDK_CONTROL_MASK)
{
    printf("Control key was pressed");
}

Alternatively, you can use macros (or their values, but please don't) defined in gdkkeysyms.h

#define GDK_KEY_Shift_L 0xffe1

#define GDK_KEY_Shift_R 0xffe2

#define GDK_KEY_Control_L 0xffe3

#define GDK_KEY_Control_R 0xffe4

switch(event->keyval) {
    case GDK_KEY_Shift_L:
        printf("Left shift key was pressed");
    break;
    case GDK_KEY_Shift_R:
        printf("Right shift key was pressed");
    break;
    case GDK_KEY_Control_L:
        printf("Left control key was pressed");
    break;
    case GDK_KEY_Control_R:
        printf("Right control key was pressed");
    break;
}
\$\endgroup\$
  • \$\begingroup\$ GDK_SHIFT_MASK is an enum / POUND define macro, what is the numeric value \$\endgroup\$ – Julian Tiemann Jul 13 at 22:34
  • \$\begingroup\$ i like your answer, but it does not quite answer my question \$\endgroup\$ – Julian Tiemann Jul 13 at 22:36
  • \$\begingroup\$ Also, does this GDK stuff fit in a char? \$\endgroup\$ – Julian Tiemann Jul 13 at 22:37
  • 1
    \$\begingroup\$ @JulianTiemann The answer to your question is that there is no ascii character that represents "shift" or "control". These are keyboard states. \$\endgroup\$ – Alexandre Vaillancourt Jul 13 at 22:45
  • \$\begingroup\$ @JulianTiemann edited the answer \$\endgroup\$ – Ocelot Jul 13 at 22:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.