// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
-#if NET_2_0
+#if !NET_2_1
using System.Runtime.InteropServices;
using System.Text;
namespace System {
public bool Visible;
}
+#pragma warning disable 169
struct InputRecord {
public short EventType;
// This is KEY_EVENT_RECORD
bool pad2;
//
}
+#pragma warning restore 169
struct CharInfo {
public char Character;
IntPtr inputHandle;
IntPtr outputHandle;
short defaultAttribute;
- bool inited;
public WindowsConsoleDriver ()
{
}
}
- public bool Echo { // not really used on windows
- get { return true; }
- set {}
- }
-
public ConsoleColor ForegroundColor {
get {
ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
public bool TreatControlCAsInput {
get {
int mode;
- if (!GetConsoleMode (outputHandle, out mode))
+ if (!GetConsoleMode (inputHandle, out mode))
throw new Exception ("Failed in GetConsoleMode: " + Marshal.GetLastWin32Error ());
// ENABLE_PROCESSED_INPUT
set {
int mode;
- if (!GetConsoleMode (outputHandle, out mode))
+ if (!GetConsoleMode (inputHandle, out mode))
throw new Exception ("Failed in GetConsoleMode: " + Marshal.GetLastWin32Error ());
bool cAsInput = ((mode & 1) == 0);
if (value)
mode &= ~1;
else
- mode++;
+ mode |= 1;
- if (!SetConsoleMode (outputHandle, mode))
+ if (!SetConsoleMode (inputHandle, mode))
throw new Exception ("Failed in SetConsoleMode: " + Marshal.GetLastWin32Error ());
}
}
SmallRect region = new SmallRect (sourceLeft, sourceTop, sourceLeft + sourceWidth - 1, sourceTop + sourceHeight - 1);
fixed (void *ptr = &buffer [0]) {
if (!ReadConsoleOutput (outputHandle, ptr, bsize, bpos, ref region))
- throw new ArgumentException ("", "Cannot read from the specified coordinates.");
+ throw new ArgumentException (String.Empty, "Cannot read from the specified coordinates.");
}
int written;
bpos = new Coord (0, 0);
region = new SmallRect (targetLeft, targetTop, targetLeft + sourceWidth - 1, targetTop + sourceHeight - 1);
if (!WriteConsoleOutput (outputHandle, buffer, bsize, bpos, ref region))
- throw new ArgumentException ("", "Cannot write to the specified coordinates.");
+ throw new ArgumentException (String.Empty, "Cannot write to the specified coordinates.");
+ }
+
+ public void Init ()
+ {
+ }
+
+ public string ReadLine ()
+ {
+ StringBuilder builder = new StringBuilder ();
+ bool exit = false;
+ do {
+ ConsoleKeyInfo key = ReadKey (false);
+ char c = key.KeyChar;
+ exit = (c == '\n');
+ if (!exit)
+ builder.Append (key.KeyChar);
+ } while (!exit);
+ return builder.ToString ();
}
public ConsoleKeyInfo ReadKey (bool intercept)
{
int eventsRead;
InputRecord record = new InputRecord ();
- do {
+ for (;;) {
if (!ReadConsoleInput (inputHandle, out record, 1, out eventsRead))
throw new InvalidOperationException ("Error in ReadConsoleInput " +
Marshal.GetLastWin32Error ());
- } while (record.EventType != 1);
+ if (record.KeyDown && record.EventType == 1 && !IsModifierKey (record.VirtualKeyCode))
+ break;
+ }
// RIGHT_ALT_PRESSED 1
// LEFT_ALT_PRESSED 2
throw new ArgumentOutOfRangeException ("left/top", "Windows error " + Marshal.GetLastWin32Error ());
}
+ private bool IsModifierKey(short virtualKeyCode)
+ {
+ // 0x10 through 0x14 is shift/control/alt/pause/capslock
+ // 0x2C is print screen, 0x90 is numlock, 0x91 is scroll lock
+ switch (virtualKeyCode) {
+ case 0x10:
+ case 0x11:
+ case 0x12:
+ case 0x13:
+ case 0x14:
+ return true;
+ case 0x2C:
+ case 0x90:
+ case 0x91:
+ return true;
+ default:
+ return false;
+ }
+ }
+
//
// Imports
//