Keyboard Hooks and Menu Activation

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.
LVL 2
LordWolfyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pramod BugudaiCommented:
Hi. Follow this link. This will help you.
http://delphi.about.com/od/windowsshellapi/a/keyboard_hook.htm
0
aikimarkCommented:
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
LordWolfyAuthor Commented:
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

aikimarkCommented:
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
LordWolfyAuthor Commented:
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
LordWolfyAuthor Commented:
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
LordWolfyAuthor Commented:
Increasing points...
0
Pramod BugudaiCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LordWolfyAuthor Commented:
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
LordWolfyAuthor Commented:
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
LordWolfyAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.