GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Aug-2004, 08:06
Alhazred Alhazred is offline
Awaiting Email Confirmation
 
Join Date: Jul 2004
Location: Italy
Posts: 17
Alhazred will become famous soon enough

[C] Function keys


CPP / C++ / C Code:
char * UserInput(int max) {
  BOOL EndInput = TRUE;
  unsigned int Count;
  int Count2 = 0, Count3 = 0, cursore = 0;
  char AllowedCh[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', '\0'};
  char AllowedNum[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '\0'};
  char *Buff;
  char Current = ' ';
  char provvisorio[2];
  char *definitivo;

  Buff = (char *) malloc((max + 1));
  definitivo = (char *) malloc((max + 1));
  Buff[0] = '\0';
  definitivo[0] = '\0';

  while(EndInput)
  {
      provvisorio[Count3] = getch();
      definitivo = strupr(provvisorio);
      definitivo[Count3 + 1] = '\0';
      Current = definitivo[Count3];
      switch(Current)
      {
         case 13: EndInput = FALSE;
                  Buff[Count2] = '\0';
                  break;

         case 8: Buff[Count2] = '\0';
                 if(cursore > 0)
                 {
                   printf("\b \b");
                   Count2--;
                   Buff[Count2] = '\0';
                   cursore--;
                 }
                 break;

         /*function keys exclusion*/
         case(0+65): break;
         case(0+66): break;
         case(0+67): break;
         case(0+68): break;
         case(0+69): break;
         case(0+70): break;
         case(0+71): break;
         case(0+72): break;
         case(0+73): break;
         case(0+74): break;
         /****************/

         default: if(max == 1)
                  {
                      for(Count = 0; Count < strlen(AllowedCh); Count++)
                      {
                         if(Current == AllowedCh[Count] && (Count2 < max))
                         {
                             printf("%c", Current);
                             Buff[Count2] = Current;
                             Count2++;
                             cursore++;
                          }
                      }
                      break;
                  }
                  else
                  {
                      for(Count = 0; Count < strlen(AllowedNum); Count++)
                      {
                         if(Current == AllowedNum[Count] && (Count2 < max))
                         {
                             printf("%c", Current);
                             Buff[Count2] = Current;
                             Count2++;
                             cursore++;
                         }
                      }
                      break;
                  }
      }
  }
  return Buff;
} //End UserInput

I have a problem with this code, if I press F7, F8, F9, F10, pagup, home introduce valid character for input.
Well have you some suggestion about how to recognize if a function key or a char key is pressed and accept only the chars?
How to do in my code?

Example, if I press F7 it will input A. I don't want this!
Last edited by JdS : 08-Aug-2004 at 10:19. Reason: Please insert [c] & [/c] tags between your example C codes
  #2  
Old 07-Aug-2004, 09:57
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Since you are using the non-standard C-function getch(), there may not be much published on how to handle function keys. In fact, for my compiler (Borland bcc32 version 5.1.1), function keys return multiple characters to the calling program.

To see what happens with function keys, try the following. I compiled and executed with Borland bcc32 version 5.1.1. If you use a different compiler, the results may be different, so you must try it yourself, rather than have me tell you what I got for <F7>, <Home>, etc.

CPP / C++ / C Code:
#include <conio.h>
#include <stdio.h>
int main()
{
  int inch;

  printf("\n\n");
  printf("Key-press values are shown in hex. Press Enter or ctrl-c to exit\n");
  printf("\n\n");
  do {
    inch = getch();
    printf ("%02x ", inch);
  } while ((inch != 0x03) && (inch != '\n') && (inch != '\r'));
  printf("\n\n");
  return 0;
}

There are a couple of bothersome things in your program.

For example
CPP / C++ / C Code:
      definitivo = strupr(provvisorio);

This sets the value of the pointer definitivo to the value of the pointer provvisorio. If you want to copy one string to the other, use strcpy() or something equivalent. (I assume you meant to copy, otherwise why use malloc() to get a block of memory for definitivo?) Also, look at the next point.

You use malloc() twice, but never free() the memory that was obtained.
Note that you can never free definitivo in your program, since you don't save the value of the pointer that malloc() gave you.

Best regards,

Dave
  #3  
Old 07-Aug-2004, 10:47
Alhazred Alhazred is offline
Awaiting Email Confirmation
 
Join Date: Jul 2004
Location: Italy
Posts: 17
Alhazred will become famous soon enough
I already know that the function keys returns two values, i.e. F7 returns 0 and 65, but if leave the code as I wrote, function keys doesn't enter a character as input, but the char isn't inserted neigther by pressing a,b,c...
I really doesn't know how to solve. Please tell me if you know.

I need "definitivo = strupr(provvisorio);" 'couse I want to change in uppercase the user input, would you do it in another way? How?


P.S. I'm using Visual C++ 6.
  #4  
Old 07-Aug-2004, 14:02
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Well, your code calls getch() and acts on the byte it returned. Since function keys return 2 bytes, you have to call getch() twice for function keys. How do you know whether you need to call getch() again? Well it depends on what the first call gave.

Now, remember getch() is not a C standard library function, so different implementations may give different results. Did you compile and run the previous program that I gave you? If you did, then you could see that, for example, pressing <F1> returns a value of 0x00 for the first call to getch() and a value of 0x3b for the next call. (It went through the loop twice and printed out two values when I pressed <F1>.) Note that for certain keys, such as <Home>, Borland returns 0x00 for the first byte, but Microsoft returns 0xe0 for the first byte.

That is for my keyboard and my system. You may get different results with other input devices. That's why I suggest you run the previous program on your system so that you can identify whatever keys you want to use.

Here's an example that shows what I have in mind. Keep in mind that you may have to do something differen, but at least here is something that works for me (for both Borland and Microsoft compilers on Windows XP and a Microsoft Ergonomic US-Style Keyboard).

CPP / C++ / C Code:
#include <conio.h>
#include <stdio.h>
int main()
{
  int inch;

  printf("\n\n");
  printf("Key-press values are shown in hex. Press Enter or ctrl-c to exit\n");
  printf("\n\n");
  do {
    inch = getch();
    if ((inch == 0) || (inch == 0xe0)) {
      printf("You pressed a function key. The first byte was 0x%02x,", inch);
      inch = getch();
      printf(" the second byte is 0x%02x\n", inch);
    }
    else {
      printf("Not a function key, the value is 0x%02x\n", inch);
    }
  } while ((inch != 0x03) && (inch != '\n') && (inch != '\r'));
  printf("\n\n");
  return 0;
}


Of course you can use switch() or nested if -- else -- statements to classify and operate on the key codes.

Finally: strupr() is also not a part of the C-Standard library, but the implementations that I am familiar with act the same way: they convert the string to upper case (The string whose pointer you gave it) and they return a pointer to that same string.

so

CPP / C++ / C Code:
  strupr(provvisorio);

converts the string to upper case. If you want to copy it to definitivo, then just say

CPP / C++ / C Code:
  strcpy(definitivo,provvisorio);

If you want to leave provvisorio unchanged and to put an upper-case converted version in provvisorio, then just do this:

CPP / C++ / C Code:
  strcpy(definitivo,provvisorio);
  strupr(definitivo);

Now, I haven't tried to follow the logic of your program, so I have no idea if it needs further fixes, but I hope this helps you to get a little farther along.

Regards,

Dave
  #5  
Old 07-Aug-2004, 16:32
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Your problem is you're not checking for a function key in the proper place. You have
CPP / C++ / C Code:
case(0+65): break;
which simply says case 'A'

If you want to exclude ALL function keys,
CPP / C++ / C Code:
while(EndInput)
{
    provvisorio[Count3] = getch();
    if (provvisorio[Count3] == 0)  // test if a function key
    {
        getch();    // read the character and ignore it
    }
    // At this point if a function key was pressed then
    //    provvisorio[Count3] will be 0.

    definitivo = strupr(provvisorio);
    definitivo[Count3 + 1] = '\0';
__________________

Age is unimportant -- except in cheese
  #6  
Old 07-Aug-2004, 20:27
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by WaltP
If you want to exclude ALL function keys,
CPP / C++ / C Code:
.
.
.
while(EndInput)
{
 
.
.

    if (provvisorio[Count3] == 0)  // test if a function key
    {
        getch();    // read the character and ignore it
    }
.
.
.

Note that the Original Poster wants to trap "Home" and "Page Up", and he is using Visual C++. On my computer with Visual C++, "Home" gives the following two bytes: 0xe0, 0x47. "Page Up" gives 0xe0, 0x49. That's what I was trying to emphasize: it's implementation-dependent. You must test it with your own setup.

Dave


Dave
  #7  
Old 08-Aug-2004, 02:03
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by davekw7x
Note that the Original Poster wants to trap "Home" and "Page Up", and he is using Visual C++. On my computer with Visual C++, "Home" gives the following two bytes: 0xe0, 0x47. "Page Up" gives 0xe0, 0x49. That's what I was trying to emphasize: it's implementation-dependent. You must test it with your own setup.

Nope, I didn't note that... Then change to code to:
CPP / C++ / C Code:
while(EndInput)
{
    provvisorio[Count3] = getch();
    if (provvisorio[Count3] == 0)  // test if a function key
    {
        ch = getch();    // read the function character and process it
        switch(ch)
        {
            case 'A':
                // process function key 
                break;
            case 'B':
                // process function key 
                break;
        }
    }
    // At this point if a function key was pressed then
    //    provvisorio[Count3] will be 0.

    definitivo = strupr(provvisorio);
    definitivo[Count3 + 1] = '\0';

As for 0xE0, yes I've seen that as the initial byte for a function key, but I've never seen it in my implementations of C, including Visual C. The only place I remember seeing it if I remember correctly was Alt-F11 and Alt-F12.

But it you see it elsewhere, I agree -- test it before relying on 0.
__________________

Age is unimportant -- except in cheese
  #8  
Old 08-Aug-2004, 04:35
Alhazred Alhazred is offline
Awaiting Email Confirmation
 
Join Date: Jul 2004
Location: Italy
Posts: 17
Alhazred will become famous soon enough
I have removed all the case(0+...) and repleaced with
case 0: getch();

Now it works.

I have repleaced the strupr() with toupper(), so I don't use other pointers and memory.
  #9  
Old 08-Aug-2004, 20:08
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by Alhazred
I have removed all the case(0+...) and repleaced with
case 0: getch();

Now it works.

I have repleaced the strupr() with toupper(), so I don't use other pointers and memory.
That works...
If you need to actually test for specific function keys, simply use another switch within the 0-case.
__________________

Age is unimportant -- except in cheese
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 3) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
foreign keys using phpmyadmin jlee MySQL / PHP Forum 6 14-Apr-2008 20:00
Including Maps and strings?? maddie C++ Forum 17 05-Jul-2004 06:25
Keys pressing procceding Ilya C Programming Language 0 13-Sep-2003 10:30

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:33.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.