Solved

Accessing function key in Visual C++ SDI

Posted on 2000-04-28
9
335 Views
Last Modified: 2012-05-04
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?
0
Comment
Question by:joex
  • 5
  • 4
9 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 2760580
The window with the focus will get a WM_KEYDOWN/WM_KEYUP message pair when the key is pressed.  You can simply handle the messages in the window's window procedure.  

Dependig on what you are doing, you can also define a keyboard accelerator to convert the key to a command, in which case a WM_COMMAND message will be sent to the window with the focus.

Let me know if you have any questions.   More details from you might also help.
0
 

Author Comment

by:joex
ID: 2760724
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?
0
 
LVL 22

Expert Comment

by:nietod
ID: 2760823
>> 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?
0
 

Author Comment

by:joex
ID: 2761003
>>>> 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.  
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 22

Expert Comment

by:nietod
ID: 2761126
>> 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.
0
 

Author Comment

by:joex
ID: 2764457
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.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2764466
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.  
0
 

Author Comment

by:joex
ID: 2764875
I would like to know how you define commands for these function keys and keyboard accelerators to convert the function keys to commands
0
 
LVL 22

Expert Comment

by:nietod
ID: 2765547
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now