Link to home
Start Free TrialLog in
Avatar of joex
joex

asked on

Accessing function key in Visual C++ SDI

A Visual C++ SDI application running under Windows NT 4.0 needs to get control when a user presses one of the function keys on the keyboards, such as: F1, F2, F3.

How can this be done?
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of joex
joex

ASKER

After registering for the WM_KEYDOWN event, the event handler does not get control when one of the keyboard entries, such as the function key is pressed.
 
Is it possible for one application to field keyboard inputs, even if another application has the focus?
>> the event handler does not get control
Does the window that is to be handling the key have the focus?  what sort of window is this?

>> Is it possible for one application to field
>> keyboard inputs, even if another
>> application has the focus?
It is possible, but a lot of work.  You wouldn't want to do this if yuou could avoid it.   Is that the case you have?
Avatar of joex

ASKER

>>>> the event handler does not get control
                   
>>Does the window that is to be handling the key have the focus?  what sort of window is this?

I tried in both a SDI application and a dialog-based application.  


>>>> Is it possible for one application to field
>>>> keyboard inputs, even if another
>>>> application has the focus?

>>It is possible, but a lot of work.  You wouldn't want to do this if yuou could avoid it.   Is that the
                    case you have?

Possibly.  The users don't even want to have to pop-up a dialog.  They just want to hit function keys.  
>> I tried in both a SDI application and a dialog-based application.  
My point is that a windows appliction usually has many windows open at a time (remember that even though a user may consier only the big movable window a window many other things are windows too, a button, edit box, static text box etc are all windows.)  So you probalby have many windows in your app (either SDI or dialog).  Only the window with the focus will get the keyboard message.  This is usually desirable.  Usually you just control the focus so that the messages are sent to the window you want.  so the question is, what window has the focus, have you done anything to insure that, or at least why does that window have the focus? Can the focus change?  what window should be handling these messages.  whould it handle them always, or only when it has the focus?

the most common behavior is that the a window responds to a certain key only when it has the focus and not when it doesn't.  However, if you have a case where an action should be performed no matter what window has the focus, it is likely that you want to use a keyboard accelerator to run a command.  That is also failrly  common.

>> The users don't even want to have to
>> pop-up a dialog.  They just want to hit
>> function keys.
What do you mean?  What is supposed to be mappening?  

I need to understand your situation much better.
Avatar of joex

ASKER

It is not possible to control the focus via some modal dialog or something like that.  Users are going from a touch panel based design to one where the control comes from a GUI.  However, they do not want to pop up dialogs to change  camera presets.  They just want them to be available from function keys.  It is possible that they do not want to have the focus to be placed in the GUI window in order to change presets, but this is a tbd.
I still don't understand the situation well enoguh.  But woudl ti be reasonable to say that the function keys should be available at all times when any window of your application has the focus, not just a particular window.  If so and if there are a limited number of function keys you need to handle  (less than say 20) you probably should just define commands for these function keys and keyboard accelerators to convert the function keys to commands.  
Avatar of joex

ASKER

I would like to know how you define commands for these function keys and keyboard accelerators to convert the function keys to commands
A command is simply a number that is sent as a parameter with the WM_COMMAND message.   you define commands by creating unique numbers or IDs for the commands you need to support.  Typically this is done by creating constants that have unique numbers, like

const int OpenCommand = 1001;
const int CloseCommand = 1002;
const int SaveCommand = 1003;

If you have menus in your program, you probaly are already creating commands.  VC may have some sort of wizzard or other feature that allows you to create commands in a fancy, more automated ways, I don't know.  

An acceletorator links a function key to a command.  To create an accerator you can create an acceletor entry in the program's resource source file.  You can look up "ACCELERATORS Resource" in the VC help for a description of how to do this along with samples.  VC does have an automated way to create accelerator resources.  Go to the "insert" menu and then sellect "resources...".  From the list in the dialog choose "accelerator".

For an overview of accelerators and commans go to the help in the table of contents at

Platform SDK
   User Interface Services
       User Input
          Keyboard Accelerators

and then read forward (down?) from there.  There are about 20 "pages" there on it.