Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,485 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,734 people online right now. Registration is fast and FREE... Join Now!




Hi, there... how do you assign events to particular keys on the keyboa

2 Pages V  1 2 >  
Reply to this topicStart new topic

Hi, there... how do you assign events to particular keys on the keyboa

dreambaff0
28 Feb, 2008 - 09:51 AM
Post #1

New D.I.C Head
*

Joined: 24 Feb, 2008
Posts: 22

How do you assign a key on the keyboard, for example A, on a program based on the c++ language?(Example press esc to quit.)
User is offlineProfile CardPM
+Quote Post

bodom658
RE: Hi, There... How Do You Assign Events To Particular Keys On The Keyboa
28 Feb, 2008 - 10:39 AM
Post #2

D.I.C Head
**

Joined: 22 Feb, 2008
Posts: 153



Thanked: 5 times
My Contributions
If you are running windows, you can do something like this:

cpp

#include<windows.h>

void main(){

short esc;

while(!esc){
esc = GetAsyncKeyState( VK_ESCAPE );
}


This post has been edited by bodom658: 28 Feb, 2008 - 10:39 AM
User is offlineProfile CardPM
+Quote Post

dreambaff0
RE: Hi, There... How Do You Assign Events To Particular Keys On The Keyboa
3 Mar, 2008 - 07:28 AM
Post #3

New D.I.C Head
*

Joined: 24 Feb, 2008
Posts: 22

It says that the main function has an error, and therefor I can't compile the file.
User is offlineProfile CardPM
+Quote Post

letthecolorsrumble
RE: Hi, There... How Do You Assign Events To Particular Keys On The Keyboa
3 Mar, 2008 - 07:46 AM
Post #4

Student of The Sun
Group Icon

Joined: 7 Nov, 2007
Posts: 550



Thanked: 1 times
My Contributions

Try posting some of your code so we can be more helpful.

This post has been edited by letthecolorsrumble: 3 Mar, 2008 - 07:56 AM
User is offlineProfile CardPM
+Quote Post

letthecolorsrumble
RE: Hi, There... How Do You Assign Events To Particular Keys On The Keyboa
3 Mar, 2008 - 08:02 AM
Post #5

Student of The Sun
Group Icon

Joined: 7 Nov, 2007
Posts: 550



Thanked: 1 times
My Contributions
cpp

#include <windows.h>


void main(){
short esc=0;

while(!esc){
esc = GetAsyncKeyState( VK_ESCAPE );
}
}


This should work for you, bodom658 missed to initialize esc to zero and a } to end main(); . Not a big deal. wink2.gif
User is offlineProfile CardPM
+Quote Post

dreambaff0
RE: Hi, There... How Do You Assign Events To Particular Keys On The Keyboa
5 Mar, 2008 - 02:35 AM
Post #6

New D.I.C Head
*

Joined: 24 Feb, 2008
Posts: 22

QUOTE(letthecolorsrumble @ 3 Mar, 2008 - 09:02 AM) *

cpp

#include <windows.h>


void main(){
short esc=0;

while(!esc){
esc = GetAsyncKeyState( VK_ESCAPE );
}
}


This should work for you, bodom658 missed to initialize esc to zero and a } to end main(); . Not a big deal. wink2.gif
Thanks, letthecoloursrumble.
So what you did is assign the variable esc a value of zero and then apointed the key escape to that variable. My question is now, if I wanted to change the key esc to another key, for instance q, I should write:
esc = GetAsyncKeyState( VK_q )?
User is offlineProfile CardPM
+Quote Post

letthecolorsrumble
RE: Hi, There... How Do You Assign Events To Particular Keys On The Keyboa
5 Mar, 2008 - 04:21 AM
Post #7

Student of The Sun
Group Icon

Joined: 7 Nov, 2007
Posts: 550



Thanked: 1 times
My Contributions
cpp

#include <windows.h>
#include <iostream>
using namespace std;
int main(){

short esc=0;

while(!esc){
cout<<"Waiting"<<endl;
esc = GetAsyncKeyState( 'Q' );
}

return 0;
}


So which every alphabet you want to detect, you just need to change the code accordingly, but they have to be in 'UPPERCASE'. Test it with different case and you will learn smile.gif
User is offlineProfile CardPM
+Quote Post

dreambaff0
RE: Hi, There... How Do You Assign Events To Particular Keys On The Keyboa
5 Mar, 2008 - 04:42 AM
Post #8

New D.I.C Head
*

Joined: 24 Feb, 2008
Posts: 22

Thanks...

LAst qustion and I will leave you alone... (Just to hunt you down on another matter, ;-))

If i want to trigger a function to work on taht I just need to state that bit of code and initialise the if statement wthin the while loop, yes?
User is offlineProfile CardPM
+Quote Post

letthecolorsrumble
RE: Hi, There... How Do You Assign Events To Particular Keys On The Keyboa
5 Mar, 2008 - 04:48 AM
Post #9

Student of The Sun
Group Icon

Joined: 7 Nov, 2007
Posts: 550



Thanked: 1 times
My Contributions
Yes that is the way to do it! Happy Coding! smile.gif
User is offlineProfile CardPM
+Quote Post

dreambaff0
RE: Hi, There... How Do You Assign Events To Particular Keys On The Keyboa
5 Mar, 2008 - 04:56 AM
Post #10

New D.I.C Head
*

Joined: 24 Feb, 2008
Posts: 22

QUOTE(letthecolorsrumble @ 5 Mar, 2008 - 05:48 AM) *

Yes that is the way to do it! Happy Coding! smile.gif
Thanks a lot, you are a life saver... icon_up.gif

User is offlineProfile CardPM
+Quote Post

letthecolorsrumble
RE: Hi, There... How Do You Assign Events To Particular Keys On The Keyboa
5 Mar, 2008 - 05:10 AM
Post #11

Student of The Sun
Group Icon

Joined: 7 Nov, 2007
Posts: 550



Thanked: 1 times
My Contributions
I didn't know your life was at risk wink2.gif
User is offlineProfile CardPM
+Quote Post

bhandari
RE: Hi, There... How Do You Assign Events To Particular Keys On The Keyboa
5 Mar, 2008 - 05:26 AM
Post #12

D.I.C Addict
Group Icon

Joined: 31 Jan, 2008
Posts: 747


Dream Kudos: 900
My Contributions
spread the word </DIC>
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 12/2/08 07:16PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month