Wednesday, January 25, 2012

Console::ReadKey() - obtain a single from Console

  • ReadKey(): Obtains the next character or function key pressed by the user. The pressed key is displayed in the console window.
  • ReadKey(true): Obtains the next character or function key pressed by the user, NOT display the pressed key in the console window.
  • ReadKey(false): Obtains the next character or function key pressed by the user, display the pressed key in the console window.


Example:
Obtain a single from Console using Console::ReadKey()

// CLRConsole.cpp : main project file.

#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)
{
 ConsoleKeyInfo keyPressed;

 do{
  Console::WriteLine(L"press and key, or Q to quit");
  keyPressed = Console::ReadKey();
  Console::WriteLine();
  Console::WriteLine(keyPressed.KeyChar);
 }while(keyPressed.KeyChar != 'Q' && keyPressed.KeyChar != 'q');

 return 0;
}

No comments:

Post a Comment