Solved

Function Keys

Posted on 2000-03-02
14
255 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
  • 6
  • 3
  • 2
  • +2
14 Comments
 

Expert Comment

by:scooter1
Comment Utility
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
Comment Utility
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
Comment Utility
console or GUI application ?

If console then it's easy. You can use microsoft specific functions to get keystrokes.
0
 
LVL 32

Expert Comment

by:jhance
Comment Utility
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
Comment Utility
>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
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 1

Author Comment

by:kiphughes
Comment Utility
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
Comment Utility
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
Comment Utility
#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
Comment Utility
jhance,

switch(d)
0
 
LVL 32

Expert Comment

by:jhance
Comment Utility
Pacman,

Look CLOSER, you're wrong!






0
 
LVL 9

Expert Comment

by:Pacman
Comment Utility
oops, you re rigth  : )
0
 
LVL 32

Expert Comment

by:jhance
Comment Utility
>oops, you re rigth  : )

I know.  ;-)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

771 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

13 Experts available now in Live!

Get 1:1 Help Now