Advertisement

Two weird problems w/ PlaySound() in a keyboard hook

Started by May 07, 2001 01:37 AM
-1 comments, last by arsenius 23 years, 9 months ago
I was intrigued by all this talk of hooks, so I decided to make one for the keyboard, similar to one a friend of mine made a few months back (his was cooler than mine though, but i've only put a bit of time into this...) ok, heres the code. It is supposed to make a beep noise when you hit a key, and stop when you hit ctrl+break (isn't it nice that I told you the way to make it stop?)

#include 
#include 
#include 
#include 
#define WIN32_LEAN_AND_MEAN

#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000 ? 1 : 0))

HHOOK hhk = NULL;
HINSTANCE hInstance = NULL;

HOOKPROC KBHookProc(int, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev,
				   LPSTR lpCmdLine,int nCmdShow)
{
	hInstance = hInst;
	HOOKPROC (*proc)(int,WPARAM,LPARAM);
	proc = &KBHookProc
	hhk = SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)proc,NULL,NULL);
	while (!KEYDOWN(VK_CANCEL)){}
	if (hhk !=NULL)
		UnhookWindowsHookEx(hhk);
	return 0;
}

HOOKPROC KBHookProc(int code, WPARAM wparam, LPARAM lparam)
{
	PlaySound("beep0.wav", NULL, SND_ASYNC|SND_FILENAME|SND_NOSTOP);
	return (HOOKPROC)CallNextHookEx(hhk,code,wparam,lparam);
}
  
There are two problems (now that i have finaly fixed all the others). 1) Sometimes it doesn't play the sound "beep0.wav", instead playing the default windows highpitched painful "ding" noise [UPDATE] it just makes the ding noise when typing in AOL Instant Messenger 2) It only works in Debug mode, not Release. Release compiles fine, but when it executes, there are no noises at all. and a third problem that i'm not too concerned about, is that neither version works on my friend's win2k machine (i'm running win98) -arsenius 'after three days without programming, life becomes meaningless' -The Tao of Programming Edited by - arsenius on May 7, 2001 2:41:50 AM

This topic is closed to new replies.

Advertisement