Accept key strokes like Ctrl+A, Alt+1 etc etc

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shadyabhi
    New Member
    • Dec 2008
    • 18

    Accept key strokes like Ctrl+A, Alt+1 etc etc

    How can i accept key strokes like Ctrl+A etc etc ... i mean combination of 2 keys... Pls give a small peice of code to demonstrate...

    i use gcc under linux platform
  • mac11
    Contributor
    • Apr 2007
    • 256

    #2
    There are several ways to get key strokes. It will help if you can give more background on your project. Specifically, are you writing a gui or command line driven application? Also, if you're making a gui, what library are you using?

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Do all of the 2-key combinations you're interested in involve the Control key? Notice that the Shift key involves two-key operation by the user but results in only a single character going to your program. The Control key is similar. An ASCII table will show you the key codes associated with Control key combinations.

      On the other hand, there are certain keys (typically arrow keys) that are single-key operations by the user, but key sequences to the program.

      Originally posted by shadyabhi
      How can i accept key strokes like Ctrl+A etc etc ... i mean combination of 2 keys... Pls give a small peice of code to demonstrate...

      Comment

      • manontheedge
        New Member
        • Oct 2006
        • 175

        #4
        I had to write a keystroke program at some point, and the way I did it was to watch when keys were pressed and released ... so if you're getting a message ( actually in my case a ton of messages ) saying shift is being held down, then whatever comes after that is capitalized, up until the shift key is released. It's the same for any multiple key combination. As far as I know, you can't really see multiple key presses simultaneously, so you have to have some method, some logic to figure it out, but it's not too difficult.

        Comment

        • shadyabhi
          New Member
          • Dec 2008
          • 18

          #5
          i just want to make a SIMPLE command line program that can accept keystrokes like "Ctrl+a" or "Ctrl+B" or "Alt+2" or anything... Means combination of 2 keys..
          Thats is very simple when there are only keys like 1-10 numbers or alphabets a-z... i can easily use ASCII values to identify the key..

          If suppose i make a simple calculator for performing addition, substraction, multiplication or fivision..
          When i run the program, the options will be displayed like
          1. Press "Ctrl+A" to add
          2. Press "Ctrl+S" to substract..
          and so on....

          I can do it by simply using alphabets like "Press A for addition, B for substraction and so on...".
          But right now, i want to learn how that kind of key presses are detected in C language... I think i am clear enough.. Now pls reply and help... And I AM DOING ALL THAT IN LINUX using GCC as compiler so pls tell acccordingly

          Comment

          • Aftabpasha
            New Member
            • Oct 2008
            • 32

            #6
            Search for "Scan Codes" or "Keyboard Scan Codes". You can use them just like you are using your ASCII codes with some minor modifications.

            Regards,
            Aftab

            Comment

            • shadyabhi
              New Member
              • Dec 2008
              • 18

              #7
              I went through the link below..

              ASCII Chart and Other Resources

              It contains a PAIR of 2 numbers for each strokes.. How to use that... Pls guide a bit more using C code

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                Perhaps an experiment would be helpful. Write a simple program that obtains successive characters from stdin and prints their values to stdout. Print the value as two hexadecimal characters followed by a space. Then you can press various two-key combinations and see what happens. The results of your experiment, combined with the scan code and ASCII table information you found on the internet should be enough to get you going.

                Comment

                • Aftabpasha
                  New Member
                  • Oct 2008
                  • 32

                  #9
                  Same as you are accepting your normal keys. Just check the accepted keystroke. If it equals to zero then read one more keystroke.
                  i.e if ch==0 is true then again accept a key and check for required values.
                  Hope it will work for you.

                  Kind Regards,
                  Aftab

                  Comment

                  • shadyabhi
                    New Member
                    • Dec 2008
                    • 18

                    #10
                    @donbock

                    I tried exactly what you said. Do tell if i misinterpreted you Below is the code:

                    #include<stdio. h>

                    int main()
                    {
                    char ch[3];
                    scanf("%s",ch);
                    printf("%d %d",ch[0],ch[1]);
                    return 0;
                    }

                    When i enter "Alt+A", output is "-61 -127"(WITHOUT QUOTES) WHICH SHOULD ACTUALLY BE "00 30"(WITHOUT QUOTES).
                    Also, when i entered get the SAME output for all function keys. I got the output "27 79"(WITHOUT QUOTES) for all function keys.. Pls do comment on this..

                    ALSO,@ Aftabpasha, Can you be more specific in what you are saying. I tried somewhat as you, but was unable to succeed.

                    Need your help, buddies, do reply

                    Comment

                    • donbock
                      Recognized Expert Top Contributor
                      • Mar 2008
                      • 2427

                      #11
                      I meant something more like this for the experimental program that lets you probe the scan codes.
                      Code:
                      #include <stdio.h>
                      int main(void) {
                         int c;
                         for (;;) {
                            c = getchar();
                            if (c == EOF) break;
                            printf("%02X ", c);
                         }
                      }
                      A potential problem here is whether stdin is line-buffered. If so, you'll have to hit the return key before getchar receives any characters. For purposes of this test it would be better if that didn't happen. You could check if your OS supports an ioctl() opcode for controlling buffering -- I don't remember the proper opcode and I don't have the proper reference handy.

                      Comment

                      • shadyabhi
                        New Member
                        • Dec 2008
                        • 18

                        #12
                        So, thanks to you all. I did what i wanted...
                        All because of your support..

                        SPECIAL THANKS TO "DONBOCK"

                        Code:
                        #include<stdio.h>
                        
                        int main()
                        {
                            int ch;
                            int ascii[10];
                            int count=1;
                            ch=getchar();
                            ascii[0]=ch;
                            while (1)       //Loop used for storing successive ASCII values
                            {
                                ascii[count]=getchar();
                                if (ascii[count]==10) break;
                                count++;
                            }
                            if (27==ascii[0] && 79==ascii[1] && 80==ascii[2]) printf("F1 is pressed");
                            if (27==ascii[0] && 79==ascii[1] && 81==ascii[2]) printf("F2 is pressed");
                            if (27==ascii[0] && 79==ascii[1] && 82==ascii[2]) printf("F3 is pressed");
                            if (27==ascii[0] && 91==ascii[1] && 72==ascii[2]) printf("HOME key is pressed");
                            return 0;
                        }

                        Comment

                        Working...