Link to home
Start Free TrialLog in
Avatar of kiphughes
kiphughes

asked on

Function Keys

I've asked this Q before, but didn't get a satisfactory answer. I hope posting this again will help.

I want to make use of the function keys. How do I do that? What header file do I need? What function do I use? What is the syntax for it?

The last time I asked, something was mentioned about windows.h -- I have that. I'm using Win98 and Borland C++ v 4.52.

If someone can show me a sample program, that would be great. I need to be able to loop until input from keyboard is received. Then it must be able to determine whether it's a regular key or extended key (I think that's what the function keys are called, right?).

Thanks.
Avatar of scooter1
scooter1

have you tried something like:


BOOL YourDialog::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN) {
int nVirtKey = (int) pMsg->wParam;
if (nVirtKey == VK_F1)
{
///do what you want here
}

if (nVirtKey == VK_UP)
{
///do something else here
}
return CDialog::PreTranslateMessage(pMsg);

}

Standard C++ does not support function keys not does it support the type of input you are suggesting (in standard C++ the input is buffered until the user presses the enter key, that is the input is not made available to you until the user presses enter.)

So you can't do this with standard C++.  But you can use OS-specific functions and features to do this.

Is this for a 32bit windows console program?  If so you probably want to use the ReadConsole() function to read the input.  You can do this without the line buffering, so the user doesn't have to press enter.

I can elaborate on that, but first, is this for a 32 bit console?
console or GUI application ?

If console then it's easy. You can use microsoft specific functions to get keystrokes.
You DID ask this question before and that question is STILL OPEN.  The problem with getting a good answer is that you refuse to provide the experts with more background information.  It's important to know WHAT KIND OF APPLICATION YOU ARE BUILDING as the way of handling F keys depends on whether it's a GUI (i.e. has a WINDOW) or it's a command line (a.k.a. MSDOS) program.

As I posted before:

Maybe a bit of background information information on your application would be helpful.

VK_F1 is defined in winuser.h from the Windows SDK.  I'm sure that even Borland C has this include as it's fundamental.  Usually you don't include it directly but rather it's brought in by windows.h.

For a Windows GUI application you need to handle the WM_KEYDOWN message.

For a console app, the F keys return 2 chars each according to the following table:

F1  0x00 0x3b
F2  0x00 0x3c
F3  0x00 0x3d
F4  etc.
F11 0xe0 0x85
F12 0xe0 0x86



>I need to be able to loop until input
>from keyboard is received.

BTW, this is NOT how to do this in a Windows program.  It's incredibly bad programmig practice.  Never loop waiting for user input.  Set things up so that you receive a message when the user input you are waiting for happens.

It seems to me that you are unfamiliar with the most basic Windows programming concepts.  I suggest you get the book, "Programming Windows" by Charles Petzold.  It covers how to get information from the keyboard in great detail but more importantly, it will teach you how to write a Windows program.

Avatar of kiphughes

ASKER

jhance,No doubt, I am not extremely familiar with C++, or programmning for Windows for that matter. And that's why I am here asking for help.

This is a console application I'm trying to create. Suggestions Pacman?

I'd like to know what header file to include and what the syntax is for whatever function I need to be using.

jhance, you had another approach to the loop I was suggesting. Can you show me how this is done?
OK, now we know:


For a console app, the F keys return 2 chars each according to the following table:

F1  0x00 0x3b
F2  0x00 0x3c
F3  0x00 0x3d
F4  etc.
F11 0xe0 0x85
F12 0xe0 0x86

just read then with getchar()
jhance, Here's the example I got from my help file regarding getchar():

#include <stdio.h>

int main(void)
{
   int c;

/*
Note that getchar reads from stdin and is line buffered; this means it will not return until you press ENTER.
 */

   while ((c = getchar()) != '\n')
      printf("%c", c);

   return 0;
}

It returns an integer. And it also needs ENTER. I've modified the while arguement, but still int. How do I make it so that it shows those hex numbers you were talking about?
Do you want it to be line buffered?  If not you probably want to use ReadConsole()   That is what I was asking about before.

>> t returns an integer
Just treat it as a character.

But I really think you want to switch to ReadConsole(), getchar() works weirdly with fucntion keys.
ASKER CERTIFIED SOLUTION
Avatar of jhance
jhance

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

switch(d)
Pacman,

Look CLOSER, you're wrong!






oops, you re rigth  : )
>oops, you re rigth  : )

I know.  ;-)