Click here to Skip to main content
15,914,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am making a regular window in windows (not a DOS window) and I want to check if a keyboard and a "on-screen button" is being pressed in a .cpp file (the window items are set up in a .h file), something like this:

while(true)
{
     if(keyboard button 1 is being pressed)
     {
           variable = variable + 1;
     }
}


or:

while(true)
{
     if("on-screen button" is being pressed)
     {
           variable = variable + 1;
     }
}

.

Also, how can i print a variable in a label?

[Modified: added pre tags...they are your friend!]
Posted
Updated 25-Oct-11 0:15am
v5

Windows applications work differently in that it is event driven.
You do not keep polling and check for attributes as you have done.

So you generally have a dialog procedure in case of a Win32 application or a callback function in case of an MFC application for a button click. Here you write the code that needs to be done when the button is clicked.

To show the value of a variable in a label you can use the SetWindowText API.
 
Share this answer
 
Comments
Karsten Malmquist 31-Dec-10 7:24am    
In a .h file I know how to check if a button is being clicked, but I want to have variables through the entire time the window is running, then I thought I might check it in a while loop because then the variables wont get destroyed by scope.
The problem is that in a .h file the variables get destroyed because of the rules of scope.
Sorry if you think i'm an idiot, but what do you mean by SetWindowText?
When I print out text I do like this:

this->label1->Text = L"Text";
You better learn some basic windows programming. http://www.winprog.org/tutorial/[^]
anyway you could know the key state using GetAsyncKeyState function

http://msdn.microsoft.com/en-us/library/ms646293(v=vs.85).aspx[^]
 
Share this answer
 
As Superman already stated, Windows programming is event driven. In order to be able to catch those events at all, you need to register as an application, create a messaging loop, and create an addressable Window somewhere that can accept the messages. Checking a state in an infinite loop is probably not the best way to approach your problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900