fstream, getch

This is a discussion on fstream, getch within the C++ Programming forums, part of the General Programming Boards category; Since I was unaware of the forums rules stating I can't ask for help for a own personal project of ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    6

    fstream, getch

    Since I was unaware of the forums rules stating I can't ask for help for a own personal project of mine that which apparently violates forum rules, I need advice for getting key presses.

    First, getch/_getch returns a whitespace when a F1, F2, etc key is pressed. Is there a c++ function that can return ALL keys pressed?

    Second, if I want to write some characters to a file, what would be the best, most efficient way of doing that? Getting 1 key at time and then writing that to the file, or writing a chunk of char data at a time?

    Thanks.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    It's tough to answer what the most efficient way of doing something is when you haven't really said what you are doing.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    6
    I want to grab a keystroke, write it to a char variable, check if it equals F10, F11, or F12. If it is not these keys, write the char data to file and continue loop.

    I've already tried getch(), and the function keys (F1, F2, etc.) return a whitespace. I am asking if there is a c/c++ function other than getch() that supports the function keys and does not return a whitespace.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is there a c++ function that can return ALL keys pressed?
    It's not about a function that returns ALL keys, it's about how those keys are represented. If your system represents the function keys with multi-value scan codes, you're likely to get the same value for every one if you only read the leading byte. Run this program and see what happens when you hit the F keys:
    Code:
    #include <stdio.h> 
    #include <conio.h> 
    
    int main ( void )
    {
      int ch;
    
      while ( ( ch = getch() ) != 0x1B )
        printf ( "&#37;d\n", ch );
    
      return 0;
    }
    You'll probably find something like this:
    Code:
    F1:  0   59
    F2:  0   60
    F3:  0   61
    F4:  0   62
    F5:  0   63
    F6:  0   64
    F7:  0   65
    F8:  0   66
    F9:  0   67
    F10: 0   68
    F11: 224 133
    F12: 224 134
    Oddly enough, cprog's FAQ tells you about this in the "How do I: Use directional keys in a console application" article. It even offers a handy function that gives you a consistent result for all scan codes:
    Code:
    int get_code ( void )
    {
      int ch = getch();
    
      if ( ch == 0 || ch == 224 )
        ch = 256 + getch();
    
      return ch;
    }
    >Getting 1 key at time and then writing that to the file, or writing a chunk of char data at a time?
    It depends on how your implementation handles buffering. If it's done right, there shouldn't be enough of a difference to matter. But it's generally a safer bet to write chunks.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    6
    Wow prelude, you pretty much nailed exactly what I was looking for. I have much work to do now! I'll post if I encounter any problems, thanks a bunch!

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    6
    OK, want to make sure I understand this correctly.

    So, basically, when using getch(), the F keys just return simple pair of decimal values. If I wanted to check to see if, for example, the F1 key was pressed, I would compare it to that simple int value.

    Code:
    if (keystroke == 059)
    {
    // action here
    }
    After your post, I went ahead and googled "keyboard scan codes" "scan codes". There's also a hex value for the functions keys. If I were to compare using hex values, would it be the same, just reading the keystroke in to a string instead? I hope I haven't completely confused you with my response.

    If these are the dec and hex values for the function keys, how would I go about checking for them since they are in pairs?

    Code:
    F1        (00,59)    (0x00,0x3b)    F2       (00,60)    (0x00,0x3c)
    F3        (00,61)    (0x00,0x3d)    F4       (00,62)    (0x00,0x3e)
    F5        (00,63)    (0x00,0x3f)    F6       (00,64)    (0x00,0x40)
    F7        (00,65)    (0x00,0x41)    F8       (00,66)    (0x00,0x42)
    F9        (00,67)    (0x00,0x43)    F10      (00,68)    (0x00,0x44)
    F11       (00,133)   (0x00,0x85)    F12      (00,134)   (0x00,0x86)
    Last edited by mcborn; 03-24-2008 at 10:37 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  2. Pls repair my basketball program
    By death_messiah12 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2006, 05:15 AM
  3. ARGH! fstream errors
    By OttoDestruct in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2004, 11:37 PM
  4. Clearing input buffer after using getch()
    By milkydoo in forum C++ Programming
    Replies: 3
    Last Post: 07-22-2003, 12:04 AM
  5. Problems with fstreams.
    By mosdef in forum C++ Programming
    Replies: 7
    Last Post: 06-19-2002, 04:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21