![]() |
|
#1
|
|||
|
|||
[C] Function keysCPP / C++ / C Code:
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
|
|||
|
|||
|
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:
There are a couple of bothersome things in your program. For example CPP / C++ / C Code:
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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:
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:
converts the string to upper case. If you want to copy it to definitivo, then just say CPP / C++ / C Code:
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:
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
|
||||
|
||||
|
Your problem is you're not checking for a function key in the proper place. You have
CPP / C++ / C Code:
If you want to exclude ALL function keys, CPP / C++ / C Code:
__________________
Age is unimportant -- except in cheese |
|
#6
|
|||
|
|||
|
Quote:
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
|
||||
|
||||
|
Quote:
Nope, I didn't note that... Then change to code to: CPP / C++ / C Code:
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
|
|||
|
|||
|
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
|
||||
|
||||
|
Quote:
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 GIDBlog
Developing GUIs with wxPython (Part 3) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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