Console Problem

Started by
5 comments, last by Colin Jeanne 19 years, 6 months ago
I've been playing around with consoles, and I've written a small test program. It draws @s when an arrow key is pressed, and will not draw when shift is pressed.
#include <windows.h>
#include <iostream>
#include <stdlib.h>

#define CHECKWRITE(write) if(write == 1){                            cout << "@";} 

using namespace std;

int main(int argc, char *argv[])
{
  HANDLE hIn, hOut; //Handles, Which I have no idea what they are
  INPUT_RECORD i; //A recordkeeper of sorts for the buffer
  COORD line; //X and Y coordinates
  int free_loop = 1; //The forever loop
  int check = 1;
  DWORD purple = 0; //I guess the Input requires it
  
  hIn = GetStdHandle(STD_INPUT_HANDLE); //Does this shove in the "Input Code"?
  hOut = GetStdHandle(STD_OUTPUT_HANDLE); //See above
  
  
  line.X = 5;
  line.Y = 5;
  
   SetConsoleMode(hIn, ENABLE_PROCESSED_INPUT); //Sets in "Input Code", and allows Ctrl C break
  while(free_loop = 1) //Forever loop! // Putting a semicolon at the end of this sentacne would cause only the while to loop.
  {
     SetConsoleCursorPosition(hOut, line); //Uses "Output Code", and sets beginning at (5,1). Starts output at (5,1)
    ReadConsoleInput(hIn, &i, 1, &purple); //Uses "Input Code", reads to a record, detects one keystroke, and stores it. Detects Output
    switch(i.Event.KeyEvent.wVirtualKeyCode)
    {
     case VK_LEFT:      
          line.X--; //Endl does not work, but will if Positioning is outside the loop. It resets every time?
          CHECKWRITE(check)
          break;
     case VK_RIGHT:     
          line.X++;
          CHECKWRITE(check)
          break;
     case VK_UP:
          line.Y = line.Y - 1;
          CHECKWRITE(check)
          break;
     case VK_DOWN:
          line.Y = line.Y + 1;
          CHECKWRITE(check)    
          break;
     case VK_SHIFT:
          check = 1 - check;
          break;
    } 
      FlushConsoleInputBuffer(hIn); //No idea how this works
  }//while
 //Apparently, Ctrl C exits the program, not break out of the loop.  	
  return 0;
}
The problem is that pressing a key will do its function twice. Not only is this annoying, but it also makes shift not work. Any help? Thanks!
Advertisement
I seem to remember that arrow keys (along with other special function keys) send two messages back from the keyboard because they are extended keystrokes (non-ASCII).

I could have dreamt it, but that's what I remember.
- CheeseMonger
Quote:Original post by CheeseMonger
I seem to remember that arrow keys (along with other special function keys) send two messages back from the keyboard because they are extended keystrokes (non-ASCII).

I could have dreamt it, but that's what I remember.


In that case, I would have to change the keys to make it work properly.

By the way, how do I make it respond to the "U" key? I've tried VK_U, but that does not work.

Thanks!

~Dakar
if (key == 'u')
{
blah blah blah;
}
//-----------------------------------------One short sleepe past, wee wake eternally, And Death shall be no more, Death thou shalt die.
Probably you're picking up both the press and release events. Look at the INPUT_RECORD docs.

The virtual-key codes for A through Z are 'A' through 'Z' (always uppercase), nice and simple :)
Quote:Original post by uavfun
Probably you're picking up both the press and release events. Look at the INPUT_RECORD docs.

The virtual-key codes for A through Z are 'A' through 'Z' (always uppercase), nice and simple :)


Okay, I've just finished trying to use characters instead of arrow keys. I have the same problem.
To check to see if shift is pressed check to see if dwControlKeyState has the SHIFT_PRESSED flag, that is

(i.dwControlKeyState & SHIFT_PRESSED)

is true.

The reason that @ will be drawn twice is because you get a message for when the key is down and another for when it comes back up: one message for each change in state.

This topic is closed to new replies.

Advertisement