Solved

Function Keys

Posted on 2000-03-02
14
277 Views
Last Modified: 2010-04-02
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.
0
Comment
Question by:kiphughes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
  • +2
14 Comments
 

Expert Comment

by:scooter1
ID: 2578841
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
 
LVL 22

Expert Comment

by:nietod
ID: 2578848
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
 
LVL 9

Expert Comment

by:Pacman
ID: 2579386
console or GUI application ?

If console then it's easy. You can use microsoft specific functions to get keystrokes.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Expert Comment

by:jhance
ID: 2579495
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
 
LVL 32

Expert Comment

by:jhance
ID: 2579498
>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
 
LVL 1

Author Comment

by:kiphughes
ID: 2580776
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
 
LVL 32

Expert Comment

by:jhance
ID: 2580797
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
 
LVL 1

Author Comment

by:kiphughes
ID: 2581294
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
 
LVL 22

Expert Comment

by:nietod
ID: 2581329
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
 
LVL 32

Accepted Solution

by:
jhance earned 50 total points
ID: 2581424
#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
 
LVL 9

Expert Comment

by:Pacman
ID: 2586681
jhance,

switch(d)
0
 
LVL 32

Expert Comment

by:jhance
ID: 2587036
Pacman,

Look CLOSER, you're wrong!






0
 
LVL 9

Expert Comment

by:Pacman
ID: 2587187
oops, you re rigth  : )
0
 
LVL 32

Expert Comment

by:jhance
ID: 2587248
>oops, you re rigth  : )

I know.  ;-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

733 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