13 Replies - 1193 Views - Last Post: 05 March 2008 - 06:53 AM Rate Topic: -----

#1 dreambaff0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 24-February 08

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

Post icon  Posted 28 February 2008 - 10:51 AM

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.)
Is This A Good Question/Topic? 0
  • +

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

#2 bodom658  Icon User is offline

  • Villiage Idiom
  • member icon

Reputation: 114
  • View blog
  • Posts: 1,123
  • Joined: 22-February 08

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

Posted 28 February 2008 - 11:39 AM

If you are running windows, you can do something like this:

#include<windows.h>

void main(){

          short esc;

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

This post has been edited by bodom658: 28 February 2008 - 11:39 AM

Was This Post Helpful? 0
  • +
  • -

#3 dreambaff0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 24-February 08

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

Posted 03 March 2008 - 08:28 AM

It says that the main function has an error, and therefor I can't compile the file.
Was This Post Helpful? 0
  • +
  • -

#4 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

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

Posted 03 March 2008 - 08:46 AM

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

This post has been edited by letthecolorsrumble: 03 March 2008 - 08:56 AM

Was This Post Helpful? 0
  • +
  • -

#5 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

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

Posted 03 March 2008 - 09:02 AM

#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. ;)
Was This Post Helpful? 0
  • +
  • -

#6 dreambaff0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 24-February 08

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

Posted 05 March 2008 - 03:35 AM

View Postletthecolorsrumble, on 3 Mar, 2008 - 09:02 AM, said:

#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. ;)
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 )?
Was This Post Helpful? 0
  • +
  • -

#7 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

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

Posted 05 March 2008 - 05:21 AM

#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 :)
Was This Post Helpful? 0
  • +
  • -

#8 dreambaff0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 24-February 08

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

Posted 05 March 2008 - 05:42 AM

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?
Was This Post Helpful? 0
  • +
  • -

#9 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

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

Posted 05 March 2008 - 05:48 AM

Yes that is the way to do it! Happy Coding! :)
Was This Post Helpful? 0
  • +
  • -

#10 dreambaff0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 24-February 08

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

Posted 05 March 2008 - 05:56 AM

View Postletthecolorsrumble, on 5 Mar, 2008 - 05:48 AM, said:

Yes that is the way to do it! Happy Coding! :)
Thanks a lot, you are a life saver... :^:
Was This Post Helpful? 0
  • +
  • -

#11 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

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

Posted 05 March 2008 - 06:10 AM

I didn't know your life was at risk ;)
Was This Post Helpful? 0
  • +
  • -

#12 bhandari  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 754
  • Joined: 31-January 08

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

Posted 05 March 2008 - 06:26 AM

spread the word </DIC>
Was This Post Helpful? 0
  • +
  • -

#13 Guest_Stepler*


Reputation:

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

Posted 05 March 2008 - 06:36 AM

>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 )?
No, not so!
Whew:
#include <conio.h>   
void main()
  {	
	 int  q;
	 while((q!=113)&&(q!=81))
	  {			  
	   q=getch();
	  }   
}


This post has been edited by Stepler: 05 March 2008 - 09:33 AM

Was This Post Helpful? 0

#14 bhandari  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 754
  • Joined: 31-January 08

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

Posted 05 March 2008 - 06:53 AM

:code:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1