Solved

Accessing function key in Visual C++ SDI

Posted on 2000-04-28
9
336 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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
 
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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

777 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