Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29249 Discussions

Keeping Keyboard Focus on Main Window

pgruhn
Beginner
690 Views

Hello all,

I have a problem with the keyboard focus of my application.

The application consists of the main window, one OpenGL child window for graphical output and one control dialog.

Till nowI have managed to show some (sample)3D-bodies in the graphic window, and can interact (like rotate, switch light on/off) by the keyboard. The keyboard functions are processed via the WM_KEYDOWN message in the MainWindowProcedure.

However, I also want to be able to control my app via the control dialog. But if I use a control in my control dialog (right now only one check box for the light switch), the main window looses focus and consequently does no longer process my keyboard inputs. There is not even a WM_KEYDOWN message to the DialogProcedure, since the checkBox seems to gain the focus.

I triedchanging the focus back to the main window from my DialogControl (see below), but then my checkBox doesn't work any more, i.e. it stays active all the time.

INTEGER*4

FUNCTION CtrlDlgProc( hDlg, message, uParam, lParam )

...

SELECT CASE (message)

...

CASE (WM_COMMAND)

SELECT CASE (LOWORD(uParam))

CASE (IDC_LIGHTING)

WRITE (9,*) 'check_box'

CALL SwitchLight

ret=SetFocus(ghWndMain)

CtrlDlgProc = 0

...

END FUNCTION CtrlDlgProc

How can I intercept the keystroke message or how can I tell my application to set the keyboard focus back to the main window after it processed the dialog events?

0 Kudos
3 Replies
pgruhn
Beginner
690 Views

Hmm,

after trying different stuff for about 2 hours it somehow does work now, and in exactly the same way I tried above.Though I still don't understand what happened because I thought I had put everything back in the original state, i.e. the state of 2 hours ago. Strange things are happening...

0 Kudos
g_f_thomas
Beginner
690 Views

Justclose the dialog (Alt +F4) and focuswill shift to the main window. The Tab key should take focus off the check box to the NC area. If the dialog is nonmodal the Tab will eventually get the main window into focus, I think.

Gerry

0 Kudos
Jugoslav_Dujic
Valued Contributor II
690 Views
As you noticed, WM_KEYxxx messages go only to the window with keyboard focus. If you want the keypresses to have "global" effect, rather than depend on the focus state, instead of handling WM_Key*** within a window procedure, you can do one of:

* Define your important keys in an accelerator table resource, load it using LoadAccelerators, and insert TranslateAccelerator in your message loop, as here. See MSDN entry. Additional caveat: accelerator keys won't work if you have modal dialogs active.

* Define a global WH_KEYBOARD hook, which will monitor all keypresses that occur in your application (more precisely, thread) before they reach any message loop. See this thread (from 5th post down). Take care to test the bit 31, and to always call CallNextHookEx with appropriate arguments.



0 Kudos
Reply