?
Solved

Keyboard Hooks and Menu Activation

Posted on 2010-01-09
15
Medium Priority
?
567 Views
Last Modified: 2013-12-03
I have an edit control descended from TCustomPanel.  I am using a keyboard hook to process the input (a global variable referencing the currently focused control tells it what to do, if anything, with the input - this is set by the overridden DoEnter and DoExit methods).  Everything works as expected until you start dealing with objects outside the parent form.

As a fix for multiple forms, I added a check for Screen.ActiveControl to the hook procedure.  I'm not entirely sure how well that works, and there is still a problem with menus / shortcuts.  Ignoring the Alt key will allow a menu to activate, but afterwards, the control still gets the input instead of the menu.

I suppose it would be pretty easy to place code elsewhere such as the form of the menu to deal with it, but I want the control to be completely autonomous and not place any such requirements with the user.
0
Comment
Question by:LordWolfy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 2
  • 2
15 Comments
 
LVL 8

Expert Comment

by:Pramod Bugudai
ID: 26281173
Hi. Follow this link. This will help you.
http://delphi.about.com/od/windowsshellapi/a/keyboard_hook.htm
0
 
LVL 46

Expert Comment

by:aikimark
ID: 26283136
My approach would be to have a routine that sets a global variable to the handle (or name?) of the control that currently has focus.  All controls would need to have some code in their GotFocus event.
0
 
LVL 2

Author Comment

by:LordWolfy
ID: 26289588
pramodbugudai: I read that article before I started the thing.

aikimark:As you will see from my original post, that is exactly what I am doing (using a global variable to reference the focused object).

You both appear to have missed the point of the question, I want to SUSPEND the hook when something else needs the focus such as a menu.  The problem is that Delphi doesnt provide adequate notification of focus changes outside of a form, and I cannot find any decent information on how to do it via the windows api.
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 46

Expert Comment

by:aikimark
ID: 26289752
I'm still confused about the nature of this problem.  I'd done some reading along these lines:
http://delphi.about.com/od/vclusing/a/coloringfocused.htm
http://www.daniweb.com/forums/thread109568.html#

But now I'm not sure whether the focus is transferring to a different form within the same application, a different application, or just some other controls on your form.  I'm going to continue monitoring this thread.  If I learn enough about your problem, I might be able to contribute to its solution.
0
 
LVL 2

Author Comment

by:LordWolfy
ID: 26289887
I really don't know how much more simly I can put it.

- The control itself and the associated keyboard hook work perfectly.

- Changing focus to another control in the same form works perfectly because the correct methods are triggered, allowing the global variable to be set to either an instance of my edit control or to NIL.

- Changing focus to a control in another form does NOT result in these methods being invoked (this is an issue with the behaviour of Delphi).  Some extra code in the hook function to check Screen.ActiveControl partially solves this problem.

- The one issue that I don't have a solution for is when something like a menu is activated.  If, for example I tell the hook function not to process the alt key, then the menu is activated, but from that point onwards, the control still thinks it has focus, so if you select a menu option with the keyboard, you will get a text echo in the control.  No events are fired for this and even the Screen.ActiveControl check fails here.

- This control is intended to be redistributable so what I need is a solution that does not require any external code from the user (for example setting the variable in the menus activation would solve the problem right away but this is NOT acceptable - it has to be completely self-contained).
0
 
LVL 2

Author Comment

by:LordWolfy
ID: 26289929
I should add that it also needs to regain focus when the menu or whatever closes.  The only references I have found are to the messages WM_ENTERMENULOOP and WM_EXITMENULOOP.  Adding methods to listen for these doesnt seem to work - plus the object in question might not be a menu - it can be anything activated by a keyboard shortcut.
0
 
LVL 2

Author Comment

by:LordWolfy
ID: 26329253
Increasing points...
0
 
LVL 8

Accepted Solution

by:
Pramod Bugudai earned 2000 total points
ID: 26378426
Not sure, but this may help. you don't need any external code or component. Ok try the following and implement it before delete the question.

http://www.delphi3000.com/articles/article_4804.asp?SK=

regards
Pramod
0
 
LVL 2

Author Comment

by:LordWolfy
ID: 26378495
Hmm - at first glance it looks like hes just encapsulated the functionality in an object.  There are a few things in there that I haven't come across and that I can look into, so I'll cancel the delete request for now and get back to you :)
0
 
LVL 2

Author Comment

by:LordWolfy
ID: 26378498
Hmm - at first glance it looks like hes just encapsulated the functionality in an object.  There are a few things in there that I haven't come across and that I can look into, so I'll cancel the delete request for now and get back to you :)
0
 
LVL 2

Author Comment

by:LordWolfy
ID: 29250009
Sorry for the extreme delay with this - I havent been able to devote any more time to it due to work etc.  

Pramodbugudai: Yours has been the most comprehensive answer so the points are yours.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question