954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Handling KeyPress Event & overriding default keypress events in windows

I want to override the normal keypress event Alt+F4 in my application.
I am using borland builder 6.
i wrote a fn for Application->OnMessage = msghandler;
In this handler I am checking for the Key State of the Alt & F4 & then if the key is pressed I am calling some fns. This works fine. But when I press Alt+F4 the function gets called a lot of times if the key is pressed normally. I want my fn to be called only once. Is the way that I have handled correct or should i try to handle this in some other way? Please help!!!

here is my code
[code]

msghandler(tagMSG &message, bool &handled)
{
short shRet;
shRet = GetAsyncKeyState(VK_MENU);
bool bVal = (shRet >> 1)&& 1;
short key = GetAsyncKeyState(VK_F4);
bool keyState = (key >> 1)&& 1;

if(bVal == true && keyState == true)
{
handled = true;
// I am calling my fns here which has to be called on press of Alt+F4
}
}
[\code]

abar
Newbie Poster
4 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

Alt-F4 is used by the system to terminate an application. Users expect this. (Don't do things that users don't expect.)

You might want to look at the OnClose and OnCanClose events if you just want to do cleanup before the application terminates or to prevent the application from terminating under some circumstances.

Be warned, though. Programs that can't be stopped make people very angry.

Good luck.

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

Alt-F4 is used by the system to terminate an application. Users expect this. (Don't do things that users don't expect.)

You might want to look at the OnClose and OnCanClose events if you just want to do cleanup before the application terminates or to prevent the application from terminating under some circumstances.

Be warned, though. Programs that can't be stopped make people very angry.

Good luck.

True one must not not manipulate the Alt+F4. But in a particular scenario, I want to override that key press to call some other function. At this particular scenario I do not want my application to be closed, instead perform my function. Also I am using the Alt+Arrow keys to call my functions which cause the same scenario which I have mentioned in my earlier post.

abar
Newbie Poster
4 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You