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.
LVL 1
kiphughesAsked:
Who is Participating?
 
jhanceCommented:
#include <conio.h>
#include <stdio.h>

void main()
{
      int c, d;
      printf("Press Keys.  Press Q to exit.\n");

      for(;;){
            c = _getch();

            if(c != 0x00 && c != 0xE0){
                  printf("Char = %x\n", c);      
                  
                  if(c == 'Q'){
                        break;
                  }
            }
            else{
                  // It's an F key
                  d = _getch();
      
                  switch(c){
                        case 0x00:
                              printf("F%d\n", d - 0x3a);
                              break;
                        case 0xE0:
                              printf("F%d\n", d - 122);
                              break;
                  }
            }
      }
}
0
 
scooter1Commented:
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);

}

0
 
nietodCommented:
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?
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
PacmanCommented:
console or GUI application ?

If console then it's easy. You can use microsoft specific functions to get keystrokes.
0
 
jhanceCommented:
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



0
 
jhanceCommented:
>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.

0
 
kiphughesAuthor Commented:
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?
0
 
jhanceCommented:
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()
0
 
kiphughesAuthor Commented:
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?
0
 
nietodCommented:
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.
0
 
PacmanCommented:
jhance,

switch(d)
0
 
jhanceCommented:
Pacman,

Look CLOSER, you're wrong!






0
 
PacmanCommented:
oops, you re rigth  : )
0
 
jhanceCommented:
>oops, you re rigth  : )

I know.  ;-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.