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
11 10543
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?
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
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.
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
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
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
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.
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
@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
donbock 2,426
Recognized Expert Top Contributor
I meant something more like this for the experimental program that lets you probe the scan codes. - #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.
So, thanks to you all. I did what i wanted...
All because of your support..
SPECIAL THANKS TO "DONBOCK" - #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;
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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.
|
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,
|
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,...
|
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...
|
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...
| |
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...
|
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,...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| | |