CPU Usage

Hello I`ve written program but I have problem.. program use about 100 % of CPU where`s the problem? here`s the code:

#include <iostream>
#include <windows.h>
#define VK_A 0x41
#define VK_B 0x42
#define VK_C 0x43
#define VK_D 0x44
#define VK_E 0x45
#define VK_F 0x46
#define VK_G 0x47
#define VK_H 0x48
#define VK_I 0x49
#define VK_J 0x4A
#define VK_K 0x4B
#define VK_L 0x4C
#define VK_O 0x4F
#define VK_P 0x50
#define VK_R 0x52
#define VK_S 0x53
#define VK_T 0x54

#define VK_Z 0x5A

using namespace std;

int main()
{

cout<<"Programn\n";

while (1){

if(GetAsyncKeyState(VK_F12)){
keybd_event(VK_F4,0xbe,0 , 0);
keybd_event(VK_S,0x9f,0 , 0);
keybd_event(VK_T,0x94,0 , 0);
keybd_event(VK_R,0x93,0 , 0);
keybd_event(VK_Z,0xac,0 , 0);
keybd_event(VK_A,0x9e,0 , 0);
keybd_event(VK_L,0xa6,0 , 0);
keybd_event(VK_RETURN,0x0d,0 , 0);

}

else if(GetAsyncKeyState(VK_F11)){

keybd_event(VK_F4,0xbe,0 , 0);
keybd_event(VK_P,0x99,0 , 0);
keybd_event(VK_O,0x90,0 , 0);
keybd_event(VK_D,0xa0,0 , 0);
keybd_event(VK_A,0x9e,0 , 0);
keybd_event(VK_J,0xa4,0 , 0);
keybd_event(VK_RETURN,0x0d,0 , 0);




}

}
}
[1516 byte] By [L@n] at [2007-11-20 0:00:12]
# 1 Re: CPU Usage
u do not know?
do u know how while loops work? its an endless while loop that has no waiting time.
Mitsukai at 2007-11-9 1:05:39 >
# 2 Re: CPU Usage
yes... but I don`t know good cpp so I have not another idea..
L@n at 2007-11-9 1:06:37 >
# 3 Re: CPU Usage
Which Windows version do you use?
SuperKoko at 2007-11-9 1:07:35 >
# 4 Re: CPU Usage
remove while loop, instead of while use any keypress handler...event to check key...

BTW which platform are you using ?

for DOS you can use TSR Programms for such thing !

where as in windows you can write the code you have written in any kepress event...instead of while loop..

Or just try putting message dispatch loop inside while...i.e

peekmessage
translatemessage
dispatchmessage

-Anant
anantwakode at 2007-11-9 1:08:44 >
# 5 Re: CPU Usage
I use windows XP
L@n at 2007-11-9 1:09:38 >
# 6 Re: CPU Usage
L@n : I suggest you use getch() or ReadConsoleInput() ( http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/readconsoleinput.asp)
SuperKoko at 2007-11-9 1:10:37 >
# 7 Re: CPU Usage
I need it not only in window on my application, in every window, every game it have to decect that I pushed F12
L@n at 2007-11-9 1:11:38 >
# 8 Re: CPU Usage
I need it not only in window on my application, in every window, every game it have to decect that I pushed F12

Three main possibilites:
The easy, clean method: RegisterHotKey (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput/keyboardinputreference/keyboardinputfunctions/registerhotkey.asp).
Note : You have to create an hidden window for that.

Second possibility : DirectInput.
Powerful, but require learning the basics of DirectInput.

Third possibility (not a good idea).
Put Sleep(80) in your while loop (80 milliseconds is a good value).
GetAsyncKeyState, should work well for that, since it is asynchronous.

I think that the first solution is the best.

PS : I didn't know that GetAsyncKeyState, under Windows XP, was working over process boundaries... I thought it was a bug/feature only found in Windows 95/98/Me
SuperKoko at 2007-11-9 1:12:41 >
# 9 Re: CPU Usage
hmm I try with hotkeys.. but I need example, I couldn`t find.. for example if F12 is pushed program will do something.. but not only one time and then exit.. I need much as I want
L@n at 2007-11-9 1:13:44 >
# 10 Re: CPU Usage
Create a hidden (or visible) window
Call RegisterHotKey
Do a message loop, using GetMessage
Handle WM_HOTKEY messages in the WindowProc.
Quit when a when the window recieve a specific message such as WM_CLOSE.
SuperKoko at 2007-11-9 1:14:42 >
# 11 Re: CPU Usage
As I said i`m a beginner so I will not do it :(
L@n at 2007-11-9 1:15:46 >
# 12 Re: CPU Usage
As I said a a beginner so I will not do it :(Is it split personality that prevents you from telling beginner-in-you to use SuperKoko's advice? It's just a matter of asking more details if you don't feel confident enough with what was said.
RoboTact at 2007-11-9 1:16:44 >
# 13 Re: CPU Usage
Anyway thanks for help..
L@n at 2007-11-9 1:17:48 >
# 14 Re: CPU Usage
OK, I tried to do it and it failed... The following works (don't look too close, I never wrote with bare API):#include <iostream>
#include <windows.h>

using namespace std;

#define CALL_THIS_TOOL 100
#define CLASS_NAME "Keyboard Event Catcher"

LRESULT CALLBACK WindowProcedure
(HWND hWnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_HOTKEY:
cout<<"hot key pressed!"<<endl;
return 0;
}
return DefWindowProc(hWnd,msg,wParam,lParam);
}

int main()
{
WNDCLASS wndClass;
ZeroMemory(&wndClass,sizeof(wndClass));
wndClass.lpfnWndProc = WindowProcedure;
wndClass.lpszClassName = CLASS_NAME;
if(!RegisterClass(&wndClass))
{
cout<<"can't register window class"<<endl;
}

HWND hWnd = CreateWindow(CLASS_NAME,"invisible message handler placeholder",SW_HIDE,0,0,100,100,0,0,0,0);
if(!hWnd)
{
cout<<"can't create window"<<endl;
}
RegisterHotKey(hWnd, CALL_THIS_TOOL, 0, VK_F11);


MSG msg;
int status;

while ((status = ::GetMessage (&msg,0,0,0))!=0)
{
if (status == -1)
return -1;
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return 0;
}But not for F12!
There's the following paragraph in RegisterHotKey documentation:Windows NT4 and Windows 2000/XP: The F12 key is reserved for use by the debugger at all times, so it should not be registered as a hot key. Even when you are not debugging an application, F12 is reserved in case a kernel-mode debugger or a just-in-time debugger is resident.(C) http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput/keyboardinputreference/keyboardinputfunctions/registerhotkey.asp
RoboTact at 2007-11-9 1:18:54 >
# 15 Re: CPU Usage
Thank you very much!
L@n at 2007-11-9 1:19:48 >
# 16 Re: CPU Usage
And.. one more question, it is possible to use more than 1 hotkey? I don`t mean two hotkeys for example to one thing, second hotkey for some another.. soemthing like case [code]WM_HotKey2:[code]
L@n at 2007-11-9 1:20:52 >
# 17 Re: CPU Usage
And.. one more question, it is possible to use more than 1 hotkey?
Sure, read documentation on RegisterHotKey function for how to apply alt/ctrl/etc modifiers:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput/keyboardinputreference/keyboardinputfunctions/registerhotkey.asp

Following modification responds to F11 and Ctrl-Alt-Up.
#include <iostream>
#include <windows.h>

using namespace std;

#define CALL_THIS_TOOL_CMD_1 101
#define CALL_THIS_TOOL_CMD_2 102

#define CLASS_NAME "Keyboard Event Catcher"

LRESULT CALLBACK WindowProcedure
(HWND hWnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_HOTKEY:
switch(wParam)
{
case CALL_THIS_TOOL_CMD_1:
cout<<"command1"<<endl;
break;
case CALL_THIS_TOOL_CMD_2:
cout<<"command2"<<endl;
break;
}
return 0;
}
return DefWindowProc(hWnd,msg,wParam,lParam);
}

int main()
{
WNDCLASS wndClass;
ZeroMemory(&wndClass,sizeof(wndClass));
wndClass.lpfnWndProc = WindowProcedure;
wndClass.lpszClassName = CLASS_NAME;
if(!RegisterClass(&wndClass))
{
cout<<"can't register window class"<<endl;
}

HWND hWnd = CreateWindow(CLASS_NAME,"invisible message handler placeholder",SW_HIDE,0,0,100,100,0,0,0,0);
if(!hWnd)
{
cout<<"can't create window"<<endl;
}
RegisterHotKey(hWnd, CALL_THIS_TOOL_CMD_1, 0, VK_F11);
RegisterHotKey(hWnd, CALL_THIS_TOOL_CMD_2, MOD_CONTROL|MOD_ALT, VK_UP);


MSG msg;
int status;

while ((status = ::GetMessage (&msg,0,0,0))!=0)
{
if (status == -1)
return -1;
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return 0;
}
RoboTact at 2007-11-9 1:21:55 >
# 18 Re: CPU Usage
Thanks againfor your help! it works very well, good luck in coding!
L@n at 2007-11-9 1:22:49 >
# 19 Re: CPU Usage
Here is an example showing usage of an hotkey and a mechanism which prevents the application from being launched more than one time.
SuperKoko at 2007-11-9 1:23:55 >
# 20 Re: CPU Usage
mechanism which prevents the application from being launched more than one time.

i use mutex for this.
Mitsukai at 2007-11-9 1:24:57 >