Thread: Simple keyboard input

  1. #1
    Politics&Cpp geek Da-Nuka's Avatar
    Join Date
    Oct 2004
    Posts
    104

    Simple keyboard input

    Hi.
    I am trying to make a simple keyboard input with Win32API!
    I want to record the keyboard input to a string, while showing the string as it makes progress. Just like a custom edit-childcontrol.

    I have tried a lot, but I allways end up with ONE character less when using backspace, or deleting TWO chars instead of one and such... You can see under how I have taught:
    Code:
    case WM_CHAR:
      switch(wParam) {
      case '\b':
    
    
      if(cursorPlace==11) {
      torf=true;
      }
    
    
      if(cursorPlace == 0) {
      buffer[cursorPlace]=' ';
      }
    
    
      if(cursorPlace > 0) {
      buffer[cursorPlace]=' ';
      cursorPlace--;
    
      }
    
      if(torf==true) {
      buffer[cursorPlace]=' ';
      torf=false;
      }
    
    
    
      break;
      case '\r':
      for(i=0; i<150; i++) {
      buffer[i]=' ';
      cursorPlace=0;
      }
      break;
      default:
      buffer[cursorPlace]=wParam;
        if(cursorPlace < 11)cursorPlace++;
        if(cursorPlace != 10) torf=false;
      break;
      }
      InvalidateRect(hwnd,NULL, true);
      return 0;
    Can someone show me a simple and correct way of doing this?

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    That code looks kinda....messy. This is more what I did:

    In the WndProc switch statement
    Code:
            case WM_KEYDOWN:
                keys[wParam]=TRUE;
                return 0;
            case WM_KEYUP:
                keys[wParam]=FALSE;
                return 0;
    In the game loop:

    Please note, textPos was predefined as an integer variable which indicates the current position in the char array, name is a char array of size 32, and keys is a global array that is modified in the wndproc.
    Code:
        if(textPos<31)
        {
            for(int a='A';a<='Z';a++)
            {
                if(keys[a])
                {
                    keys[a]=false;
                    name[textPos]=a;
                    textPos++;
                }
            }
            if(keys[VK_SPACE])
            {
                keys[VK_SPACE]=false;
                name[textPos]=' ';
                textPos++;
            }
        }
        if(keys[VK_BACK] && textPos>0)
        {
            keys[VK_BACK]=false;
            textPos--;
            name[textPos]=0;
        }

    So in other words, it might be best just to separate the typing code from the WndProc's code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help using inline assembly for keyboard input
    By sleventeen in forum C Programming
    Replies: 7
    Last Post: 05-10-2009, 01:31 AM
  2. Stealing Keyboard input
    By zort15 in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2007, 02:22 PM
  3. Hopefully simple question, input streams
    By dpro in forum C++ Programming
    Replies: 7
    Last Post: 03-09-2006, 01:59 PM
  4. Keyboard input in a Dialog
    By ksarkar in forum C++ Programming
    Replies: 1
    Last Post: 05-20-2005, 05:39 AM
  5. Replies: 2
    Last Post: 10-09-2002, 11:22 PM