10 Replies - 4501 Views - Last Post: 02 September 2006 - 06:20 AM Rate Topic: -----

#1 DeeViLiSh  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 175
  • Joined: 25-July 06

Detect without a loop

Posted 28 August 2006 - 03:50 AM

while(n != NULL)
{
	if(GetAsyncKeyState(VK_LBUTTON))
					cout<<"You clicked!";
	
	if(GetAsyncKeyState(VK_RBUTTON))
					break;
}


To detect it whenever the user clicks, I have to run a loop that detects the click yet that solution makes my computer lag.

Is there a function that sounds like : when(GetAsyncKeyState(VK_LBUTTON))
blablabla;

so that it doesn't loop like crazy :crazy:
But then, without a loop the program would shut itself down wouldn't it?

This post has been edited by DeeViLiSh: 28 August 2006 - 03:58 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Detect without a loop

#2 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Detect without a loop

Posted 28 August 2006 - 04:45 AM

Sorry buddy, loops are the way to go.
Even if you do find a function that doesn't require a loop, it has to use loops internally to capture and process such messages.

Most Event Oriented Processes need loops to be captured. (At least in the Windows Architecture...No idea about how Linux and Mac handle events...but I guess in a similar manner)

Even a simple Win32 Program (debug the executable to see what I mean) uses an infinite loop having the TranslateMessage(), DispatchMessage() and other Functions to capture and process messages.
Was This Post Helpful? 0
  • +
  • -

#3 DeeViLiSh  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 175
  • Joined: 25-July 06

Re: Detect without a loop

Posted 28 August 2006 - 06:29 AM

Well, I'm doing an API program using this loop and right after the user checks the function and clicks on the button, instant freeze until the loop stops :

if(GetAsyncKeyState ('A'))
break;


The loop is obviously the problem but I don't know how to fix it if it's the only way =S
Was This Post Helpful? 0
  • +
  • -

#4 cipherence  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 1
  • View blog
  • Posts: 260
  • Joined: 01-April 06

Re: Detect without a loop

Posted 28 August 2006 - 10:09 PM

i'm not sure i have the whole problem, but you could also do something like:
while(this==that)
{
// code that detects mouse press
Sleep(1000); // or less make sure to include Windows.h
system("cls"); // make it turn out pretty
}


if i didn't get the point i'm sorry just helping the helpful to the lost sea of helpfull people. lol
Was This Post Helpful? 0
  • +
  • -

#5 cipherence  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 1
  • View blog
  • Posts: 260
  • Joined: 01-April 06

Re: Detect without a loop

Posted 29 August 2006 - 09:15 PM

i wrote this test program using Dev-C++:
#include<iostream>
#include<windows.h>

using namespace std;

void Member();

main()
{
Member(); // just get right to the point
}
void Member()
{
	
	if(GetAsyncKeyState(VK_LBUTTON))
					cout<<"You clicked!";
	
	if(GetAsyncKeyState(VK_RBUTTON))
					cout<<"you right clicked";
}	


there is no loop in it. but, when you click, it says you clicked! alot of times until you let go.
Was This Post Helpful? 0
  • +
  • -

#6 DeeViLiSh  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 175
  • Joined: 25-July 06

Re: Detect without a loop

Posted 30 August 2006 - 12:18 AM

Lol but when you do let go, it shuts program down, I want it to stay forever until he preeses right mouse for example. Thanks anyways.
Was This Post Helpful? 0
  • +
  • -

#7 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Detect without a loop

Posted 30 August 2006 - 05:33 AM

The Use the same code in a Win32 Program or use MFC.
Was This Post Helpful? 0
  • +
  • -

#8 DeeViLiSh  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 175
  • Joined: 25-July 06

Re: Detect without a loop

Posted 30 August 2006 - 08:05 AM

Used MFC, too lazy to type it all down, I declared as a void longing all the other functions dedicated to all the buttons, checkboxs, etc but I don't see a int main() which bothers me because, I have to click a button or check something in order to activate a piece of code but I don't know to without it.
Was This Post Helpful? 0
  • +
  • -

#9 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Detect without a loop

Posted 31 August 2006 - 07:16 AM

You don't use main() in MFC.
You instead create a class that inherits the CWinApp Object and calls a CDialog object's DoModal() function in the InitInstance Method.

Something like this:
class App:public CWinApp
{
	public:
		int InitInstance()
		{
			dlgmain d; // dlgmain derived from CDialog
			d.DoModal();			
			return 1;
		}
};

App obj1;


Was This Post Helpful? 0
  • +
  • -

#10 help-linux  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 54
  • Joined: 12-August 06

Re: Detect without a loop

Posted 31 August 2006 - 03:58 PM

if you dont want it to shut down do this:

#include<iostream>
#include<windows.h>

using namespace std;

void Member();

main()
{
		   while(1)
		  {	  
				   Member(); // just get right to the point
				   Sleep(2); // stop it using loads of cpu
		   }
}
void Member()
{
 
	if(GetAsyncKeyState(VK_LBUTTON))
					cout<<"You clicked!";
	
	if(GetAsyncKeyState(VK_RBUTTON))
					cout<<"you right clicked";
}  



Was This Post Helpful? 0
  • +
  • -

#11 violent_crimson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 31-August 06

Re: Detect without a loop

Posted 02 September 2006 - 06:20 AM

coincedentally, i just used the same idea to stop my IRC client draining cpu time - it has a thread with an infinite for loop, and if the text output box isn't too big, it will just cancel and sleep for a few milliseconds;

DWORD WINAPI Manage_Text (LPVOID n)
{
	for (;;)
	{
		nLength = GetWindowTextLength (hWndEdit_Output);
		if (nLength > 15000)
		{
			int nLine;

			nLine = (int) SendMessage (hWndEdit_Output, EM_LINEINDEX, (WPARAM) 32, (LPARAM) NULL);
			
			SendMessage (hWndEdit_Output, EM_SETSEL, (WPARAM) 0, (LPARAM) nLine);
			SendMessage (hWndEdit_Output, EM_REPLACESEL, (WPARAM) FALSE, (LPARAM) NULL);
			nLength = GetWindowTextLength (hWndEdit_Output);
			
			SendMessage (hWndEdit_Output, EM_LINESCROLL, (WPARAM) NULL, (LPARAM) nLength);
		}
		else
		{
			Sleep (1);
		}
	}
	//the nLength changed because the function concatenated strings;
	return (DWORD) n;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1