Solved

Accessing function key in Visual C++ SDI

Posted on 2000-04-28
9
334 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
Comment Utility
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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
>>>> 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

744 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

11 Experts available now in Live!

Get 1:1 Help Now