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

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




Detect without a loop

 
Reply to this topicStart new topic

Detect without a loop

DeeViLiSh
post 28 Aug, 2006 - 02:50 AM
Post #1


D.I.C Head

Group Icon
Joined: 25 Jul, 2006
Posts: 175



Thanked 1 times

Dream Kudos: 575
My Contributions


CODE
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.gif
But then, without a loop the program would shut itself down wouldn't it?

This post has been edited by DeeViLiSh: 28 Aug, 2006 - 02:58 AM
User is offlineProfile CardPM

Go to the top of the page

born2c0de
post 28 Aug, 2006 - 03:45 AM
Post #2


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

Group Icon
Joined: 26 Nov, 2004
Posts: 3,905



Thanked 34 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

DeeViLiSh
post 28 Aug, 2006 - 05:29 AM
Post #3


D.I.C Head

Group Icon
Joined: 25 Jul, 2006
Posts: 175



Thanked 1 times

Dream Kudos: 575
My Contributions


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 :

CODE
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
User is offlineProfile CardPM

Go to the top of the page

cipherence
post 28 Aug, 2006 - 09:09 PM
Post #4


D.I.C Regular

Group Icon
Joined: 1 Apr, 2006
Posts: 260



Thanked 1 times

Dream Kudos: 650
My Contributions


i'm not sure i have the whole problem, but you could also do something like:
CODE
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
User is offlineProfile CardPM

Go to the top of the page

cipherence
post 29 Aug, 2006 - 08:15 PM
Post #5


D.I.C Regular

Group Icon
Joined: 1 Apr, 2006
Posts: 260



Thanked 1 times

Dream Kudos: 650
My Contributions


i wrote this test program using Dev-C++:
CODE

#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.
User is offlineProfile CardPM

Go to the top of the page

DeeViLiSh
post 29 Aug, 2006 - 11:18 PM
Post #6


D.I.C Head

Group Icon
Joined: 25 Jul, 2006
Posts: 175



Thanked 1 times

Dream Kudos: 575
My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

born2c0de
post 30 Aug, 2006 - 04:33 AM
Post #7


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

Group Icon
Joined: 26 Nov, 2004
Posts: 3,905



Thanked 34 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


The Use the same code in a Win32 Program or use MFC.
User is offlineProfile CardPM

Go to the top of the page

DeeViLiSh
post 30 Aug, 2006 - 07:05 AM
Post #8


D.I.C Head

Group Icon
Joined: 25 Jul, 2006
Posts: 175



Thanked 1 times

Dream Kudos: 575
My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

born2c0de
post 31 Aug, 2006 - 06:16 AM
Post #9


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

Group Icon
Joined: 26 Nov, 2004
Posts: 3,905



Thanked 34 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


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:
CODE

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

App obj1;
User is offlineProfile CardPM

Go to the top of the page

help-linux
post 31 Aug, 2006 - 02:58 PM
Post #10


D.I.C Head

Group Icon
Joined: 12 Aug, 2006
Posts: 54



Thanked 1 times

Dream Kudos: 50
My Contributions


if you dont want it to shut down do this:

CODE

#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";
}  

User is offlineProfile CardPM

Go to the top of the page

violent_crimson
post 2 Sep, 2006 - 05:20 AM
Post #11


New D.I.C Head

*
Joined: 31 Aug, 2006
Posts: 36


My Contributions


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;

CODE

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;
}
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 03:14AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month