473,503 Members | 10,012 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

18 New Member
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
Dec 30 '08 #1
11 10543
mac11
256 Contributor
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?
Dec 30 '08 #2
donbock
2,426 Recognized Expert Top Contributor
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.

@shadyabhi
Dec 30 '08 #3
manontheedge
175 New Member
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.
Dec 31 '08 #4
shadyabhi
18 New Member
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
Dec 31 '08 #5
Aftabpasha
32 New Member
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
Dec 31 '08 #6
shadyabhi
18 New Member
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
Dec 31 '08 #7
donbock
2,426 Recognized Expert Top Contributor
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.
Dec 31 '08 #8
Aftabpasha
32 New Member
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
Dec 31 '08 #9
shadyabhi
18 New Member
@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
Dec 31 '08 #10
donbock
2,426 Recognized Expert Top Contributor
I meant something more like this for the experimental program that lets you probe the scan codes.
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. int main(void) {
  3.    int c;
  4.    for (;;) {
  5.       c = getchar();
  6.       if (c == EOF) break;
  7.       printf("%02X ", c);
  8.    }
  9. }
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.
Dec 31 '08 #11
shadyabhi
18 New Member
So, thanks to you all. I did what i wanted...
All because of your support..

SPECIAL THANKS TO "DONBOCK"

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5.     int ch;
  6.     int ascii[10];
  7.     int count=1;
  8.     ch=getchar();
  9.     ascii[0]=ch;
  10.     while (1)       //Loop used for storing successive ASCII values
  11.     {
  12.         ascii[count]=getchar();
  13.         if (ascii[count]==10) break;
  14.         count++;
  15.     }
  16.     if (27==ascii[0] && 79==ascii[1] && 80==ascii[2]) printf("F1 is pressed");
  17.     if (27==ascii[0] && 79==ascii[1] && 81==ascii[2]) printf("F2 is pressed");
  18.     if (27==ascii[0] && 79==ascii[1] && 82==ascii[2]) printf("F3 is pressed");
  19.     if (27==ascii[0] && 91==ascii[1] && 72==ascii[2]) printf("HOME key is pressed");
  20.     return 0;
  21. }
  22.  
Jan 1 '09 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

2
10419
by: Marc R. Bertrand | last post by:
Hello, In Excel VBA, using SendKeys in a Sub procedure with a shortcut running the Sub/macro, I can replace a users's fingers and press keys for him. How can I do this in Access? I would like...
3
43925
by: Stefan | last post by:
Hy, i have an app and i must disable this combination: ALT+F4; CTRL+ALT+DEL; CTRL+ESC;ALT+TAB like this: i find something on Internet and i can block ALT+F4 protected override...
2
6402
by: active | last post by:
In VB6 I believe MouseDown had a Shift argument that told if Shift, Ctrl or Alt was pressed. How in VB.NET MouseDown do I determine if one of these keys is pressed? I could increment a global...
5
2479
by: LisaConsult | last post by:
We upgraded a vb project to vb.net and added many enhancements. The old project was not an MDI and used buttons for functions like adding a record (Alt A), deleteing a record etc. This is a data...
3
11961
by: vanya | last post by:
i have been tryin to program(javascript) to disable the following keystroke combinations CTRL+O or CTRL+L Go to a new location (O = 79 L = 76) CTRL+S Save the current page ( S = 83) CTRL+E...
2
10596
by: Phil Reynolds | last post by:
Is there a way to trap for a Ctrl or Alt key when typing in a field? I want to be able to perform an action when the user types Ctl+A or Alt+A or whatever. I noticed the KeyDown function allows you...
10
8527
by: thupham | last post by:
Dear all friend, I want disable Ctl+Alt+Del; Ctrl+Esc; Ctrl+tab, Alt+Tab, Start button, ctrl+Alt+Del, lock all keys on the keyboard. Have you ever do it in C#. Help me. Thanks for all reply.
7
2274
by: =?Utf-8?B?U3VuZGFy?= | last post by:
Hello ALL, I want some help on How to Disable the Combination Keys stokes like ALT+CTRL+DEL, ALT+TAB or any other Combination using C#. Hope for your response. -- Best Regards,
0
7267
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6976
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5566
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
729
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
372
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.