|
-
May 3rd, 2009, 08:33 PM
#1
Exiting programs
Hello! Just having a little trouble making a program right now
I've made a basic Win32 app so far, and i wan't it to close a program when i push a certain key. I made it a console app at first and tested that and it did work, but transitioning to the Win32 app is causing me trouble. I've put my code in the APIENTRY _tWinMain function which i think is the equivalent to the consoles init main function. Heres my code:
Code:
Torrent = FindWindow(NULL, _T("Vuze"));
if (KEYDOWN(VK_HOME))
{
if( Torrent = NULL )
{
ShellExecute(NULL, L"open", L"C:\\Documents and Settings\\Home\\Desktop\\Vuze\\Azureus.exe", NULL, L"C:\\", SW_SHOWNORMAL);
}
else
{
SendMessage(Torrent, WM_CLOSE, 0, 0);
}
}
and here is my little definition for the keydown if it helps thats in my stdafx.h file
Code:
#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
It seems to run and compile fine, but i never see my program close. I used that exact code (except the key part) for my console and it worked. I have my handle at the top if your wondering where that is for the name Torrent. Is there anything i'm doing wrong here?
Lastly if your wondering why i am doing this, i have a phone that runs VOIP and my main PC with Media Center. I'm watching TV, the phone rings, and it takes me 30 seconds to turn off the torrents so i can hear what the people on the phone are saying. So i'm making life easier for myself
Thank you for your help!
-Dakiato
-
May 4th, 2009, 01:42 AM
#2
Re: Exiting programs
Code:
if( Torrent = NULL )
it should be:
Code:
if( Torrent == NULL )
-
May 4th, 2009, 02:54 PM
#3
Re: Exiting programs
Ah didn't see that. Thanks for your help cilu!
-
May 4th, 2009, 07:44 PM
#4
Re: Exiting programs
Still seem to be having a problem. It won't detect that the key is down. I added a breakpoint that outputs a message to the debug screen but it never gets past the if KEYDOWN part. I've seen on few other forums that the keydown doesn't get called if dialogs or buttons are on screen, which mine has. Is there any other way to detect a key input? Thanks for your help.
-Dak
-
May 4th, 2009, 08:12 PM
#5
Re: Exiting programs
Since Win32 programs are message driven unlike console applications, you could try and handle the WM_KEYDOWN message in your main window procedure.
-
May 4th, 2009, 08:32 PM
#6
Re: Exiting programs
Yup that was the problem! Thanks so much for helping a beginner
As i always do to help other people out heres my source:
NOTE: I placed this in the LRESULT CALLBACK WndProc function
Code:
case WM_KEYDOWN:
if (KEYDOWN(VK_HOME))
{
if( Torrent == NULL )
{
ShellExecute(NULL, L"open", L"C:\\Documents and Settings\\Home\\Desktop\\Vuze\\Azureus.exe", NULL, L"C:\\", SW_SHOWNORMAL);
}
else
{
SendMessage(Torrent, WM_CLOSE, 0, 0);
}
}
break;
EDIT: One last question, is there anyway to make this so this is checked even when it is not the active window? Once that is finished everything should be great.
Once again thank you for your help!
-Dak
Last edited by dakiato; May 4th, 2009 at 08:36 PM.
-
May 4th, 2009, 08:43 PM
#7
Re: Exiting programs
Key strokes is sent to the active window only.
So could write a global keyboard hook to get keystokes in all windows.
Check SetWindowsHookEx(WH_KEYBOARD, ....
-
May 4th, 2009, 09:26 PM
#8
Re: Exiting programs
Had a look around at how to do that and it seems i need to make a DLL, which i can do, but not quite sure how to load it into the actual program. I had a look at some tutorials but most of them are really confusing and either don't explain it, or explain it really badly. Thanks again for your help!
-
May 5th, 2009, 12:08 AM
#9
Re: Exiting programs
You need to load the dll using another exe.
Once the dll is loaded it will automatically get loaded into all running processes.
-
May 5th, 2009, 05:38 PM
#10
-
May 5th, 2009, 09:21 PM
#11
Re: Exiting programs
Ok, think i'm getting the hang of hooks, but one last question. I found this code on a Keyboard hook and just wondering, will it work since it isn't in a DLL?
http://www.rohitab.com/discuss/lofiv...hp/t34071.html
It seems to compile fine, but i'm not sure then how to check if a key is down using that function with the hook. I would've used the Keyboard Hook program that was a tutorial on CodeGuru but i have an Express edition which doesn't include some essential MFC header files, and the trial is HUGE for the pro VS2008. Could i put it in the Case VK_HOME: part as i use a similar one with the original key check?
-
May 5th, 2009, 09:39 PM
#12
Re: Exiting programs
Oh and thanks for that Kirant, it would be a lot easier, but i can't really find any examples that says how to implement it. I can find it on MSDN but it just states what it does, and not how i should call it. Still very new to this language, so i'm sorry if it seems like i can't learn.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|