PageRenderTime 28ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/corlib/System/WindowsConsoleDriver.cs

https://github.com/iainlane/mono
C# | 633 lines | 487 code | 95 blank | 51 comment | 66 complexity | 4c83a980365f57fccec2101ca947a28d MD5 | raw file
  1. //
  2. // System.WindowsConsoleDriver
  3. //
  4. // Author:
  5. // Gonzalo Paniagua Javier (gonzalo@ximian.com)
  6. //
  7. // (C) 2005 Novell, Inc. (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if !NET_2_1
  29. using System.Runtime.InteropServices;
  30. using System.Text;
  31. namespace System {
  32. struct ConsoleCursorInfo {
  33. public int Size;
  34. public bool Visible;
  35. }
  36. #pragma warning disable 169
  37. struct InputRecord {
  38. public short EventType;
  39. // This is KEY_EVENT_RECORD
  40. public bool KeyDown;
  41. public short RepeatCount;
  42. public short VirtualKeyCode;
  43. public short VirtualScanCode;
  44. public char Character;
  45. public int ControlKeyState;
  46. int pad1;
  47. bool pad2;
  48. //
  49. }
  50. #pragma warning restore 169
  51. struct CharInfo {
  52. public char Character;
  53. public short Attributes;
  54. }
  55. struct Coord {
  56. public short X;
  57. public short Y;
  58. public Coord (int x, int y)
  59. {
  60. X = (short) x;
  61. Y = (short) y;
  62. }
  63. }
  64. struct SmallRect {
  65. public short Left;
  66. public short Top;
  67. public short Right;
  68. public short Bottom;
  69. public SmallRect (int left, int top, int right, int bottom)
  70. {
  71. Left = (short) left;
  72. Top = (short) top;
  73. Right = (short) right;
  74. Bottom = (short) bottom;
  75. }
  76. }
  77. struct ConsoleScreenBufferInfo {
  78. public Coord Size;
  79. public Coord CursorPosition;
  80. public short Attribute;
  81. public SmallRect Window;
  82. public Coord MaxWindowSize;
  83. }
  84. enum Handles {
  85. STD_INPUT = -10,
  86. STD_OUTPUT = -11,
  87. STD_ERROR = -12
  88. }
  89. unsafe class WindowsConsoleDriver : IConsoleDriver {
  90. IntPtr inputHandle;
  91. IntPtr outputHandle;
  92. short defaultAttribute;
  93. public WindowsConsoleDriver ()
  94. {
  95. outputHandle = GetStdHandle (Handles.STD_OUTPUT);
  96. inputHandle = GetStdHandle (Handles.STD_INPUT);
  97. ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
  98. GetConsoleScreenBufferInfo (outputHandle, out info);
  99. defaultAttribute = info.Attribute; // Not sure about this...
  100. }
  101. // FOREGROUND_BLUE 1
  102. // FOREGROUND_GREEN 2
  103. // FOREGROUND_RED 4
  104. // FOREGROUND_INTENSITY 8
  105. // BACKGROUND_BLUE 16
  106. // BACKGROUND_GREEN 32
  107. // BACKGROUND_RED 64
  108. // BACKGROUND_INTENSITY 128
  109. static ConsoleColor GetForeground (short attr)
  110. {
  111. attr &= 0x0F;
  112. return (ConsoleColor) attr;
  113. }
  114. static ConsoleColor GetBackground (short attr)
  115. {
  116. attr &= 0xF0;
  117. attr >>= 4;
  118. return (ConsoleColor) attr;
  119. }
  120. static short GetAttrForeground (int attr, ConsoleColor color)
  121. {
  122. attr &= ~15;
  123. return (short) (attr | (int) color);
  124. }
  125. static short GetAttrBackground (int attr, ConsoleColor color)
  126. {
  127. attr &= ~0xf0;
  128. int c = ((int) color) << 4;
  129. return (short) (attr | c);
  130. }
  131. public ConsoleColor BackgroundColor {
  132. get {
  133. ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
  134. GetConsoleScreenBufferInfo (outputHandle, out info);
  135. return GetBackground (info.Attribute);
  136. }
  137. set {
  138. ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
  139. GetConsoleScreenBufferInfo (outputHandle, out info);
  140. short attr = GetAttrBackground (info.Attribute, value);
  141. SetConsoleTextAttribute (outputHandle, attr);
  142. }
  143. }
  144. public int BufferHeight {
  145. get {
  146. ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
  147. GetConsoleScreenBufferInfo (outputHandle, out info);
  148. return info.Size.Y;
  149. }
  150. set { SetBufferSize (BufferWidth, value); }
  151. }
  152. public int BufferWidth {
  153. get {
  154. ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
  155. GetConsoleScreenBufferInfo (outputHandle, out info);
  156. return info.Size.X;
  157. }
  158. set { SetBufferSize (value, BufferHeight); }
  159. }
  160. public bool CapsLock {
  161. get {
  162. short state = GetKeyState (20); // VK_CAPITAL
  163. return ((state & 1) == 1);
  164. }
  165. }
  166. public int CursorLeft {
  167. get {
  168. ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
  169. GetConsoleScreenBufferInfo (outputHandle, out info);
  170. return info.CursorPosition.X;
  171. }
  172. set { SetCursorPosition (value, CursorTop); }
  173. }
  174. public int CursorSize {
  175. get {
  176. ConsoleCursorInfo info = new ConsoleCursorInfo ();
  177. GetConsoleCursorInfo (outputHandle, out info);
  178. return info.Size;
  179. }
  180. set {
  181. if (value < 1 || value > 100)
  182. throw new ArgumentOutOfRangeException ("value");
  183. ConsoleCursorInfo info = new ConsoleCursorInfo ();
  184. GetConsoleCursorInfo (outputHandle, out info);
  185. info.Size = value;
  186. if (!SetConsoleCursorInfo (outputHandle, ref info))
  187. throw new Exception ("SetConsoleCursorInfo failed");
  188. }
  189. }
  190. public int CursorTop {
  191. get {
  192. ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
  193. GetConsoleScreenBufferInfo (outputHandle, out info);
  194. return info.CursorPosition.Y;
  195. }
  196. set { SetCursorPosition (CursorLeft, value); }
  197. }
  198. public bool CursorVisible {
  199. get {
  200. ConsoleCursorInfo info = new ConsoleCursorInfo ();
  201. GetConsoleCursorInfo (outputHandle, out info);
  202. return info.Visible;
  203. }
  204. set {
  205. ConsoleCursorInfo info = new ConsoleCursorInfo ();
  206. GetConsoleCursorInfo (outputHandle, out info);
  207. if (info.Visible == value)
  208. return;
  209. info.Visible = value;
  210. if (!SetConsoleCursorInfo (outputHandle, ref info))
  211. throw new Exception ("SetConsoleCursorInfo failed");
  212. }
  213. }
  214. public ConsoleColor ForegroundColor {
  215. get {
  216. ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
  217. GetConsoleScreenBufferInfo (outputHandle, out info);
  218. return GetForeground (info.Attribute);
  219. }
  220. set {
  221. ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
  222. GetConsoleScreenBufferInfo (outputHandle, out info);
  223. short attr = GetAttrForeground (info.Attribute, value);
  224. SetConsoleTextAttribute (outputHandle, attr);
  225. }
  226. }
  227. public bool KeyAvailable {
  228. get {
  229. int eventsRead;
  230. InputRecord record = new InputRecord ();
  231. while (true) {
  232. // Use GetNumberOfConsoleInputEvents and remove the while?
  233. if (!PeekConsoleInput (inputHandle, out record, 1, out eventsRead))
  234. throw new InvalidOperationException ("Error in PeekConsoleInput " +
  235. Marshal.GetLastWin32Error ());
  236. if (eventsRead == 0)
  237. return false;
  238. //KEY_EVENT == 1
  239. if (record.EventType == 1 && record.KeyDown)
  240. return true;
  241. if (!ReadConsoleInput (inputHandle, out record, 1, out eventsRead))
  242. throw new InvalidOperationException ("Error in ReadConsoleInput " +
  243. Marshal.GetLastWin32Error ());
  244. }
  245. }
  246. }
  247. public bool Initialized { // Not useful on windows, so false.
  248. get { return false; }
  249. }
  250. public int LargestWindowHeight {
  251. get {
  252. Coord coord = GetLargestConsoleWindowSize (outputHandle);
  253. if (coord.X == 0 && coord.Y == 0)
  254. throw new Exception ("GetLargestConsoleWindowSize" + Marshal.GetLastWin32Error ());
  255. return coord.Y;
  256. }
  257. }
  258. public int LargestWindowWidth {
  259. get {
  260. Coord coord = GetLargestConsoleWindowSize (outputHandle);
  261. if (coord.X == 0 && coord.Y == 0)
  262. throw new Exception ("GetLargestConsoleWindowSize" + Marshal.GetLastWin32Error ());
  263. return coord.X;
  264. }
  265. }
  266. public bool NumberLock {
  267. get {
  268. short state = GetKeyState (144); // VK_NUMLOCK
  269. return ((state & 1) == 1);
  270. }
  271. }
  272. public string Title {
  273. get {
  274. StringBuilder sb = new StringBuilder (1024); // hope this is enough
  275. if (GetConsoleTitle (sb, 1024) == 0) {
  276. // Try the maximum
  277. sb = new StringBuilder (26001);
  278. if (GetConsoleTitle (sb, 26000) == 0)
  279. throw new Exception ("Got " + Marshal.GetLastWin32Error ());
  280. }
  281. return sb.ToString ();
  282. }
  283. set {
  284. if (value == null)
  285. throw new ArgumentNullException ("value");
  286. if (!SetConsoleTitle (value))
  287. throw new Exception ("Got " + Marshal.GetLastWin32Error ());
  288. }
  289. }
  290. public bool TreatControlCAsInput {
  291. get {
  292. int mode;
  293. if (!GetConsoleMode (inputHandle, out mode))
  294. throw new Exception ("Failed in GetConsoleMode: " + Marshal.GetLastWin32Error ());
  295. // ENABLE_PROCESSED_INPUT
  296. return ((mode & 1) == 0);
  297. }
  298. set {
  299. int mode;
  300. if (!GetConsoleMode (inputHandle, out mode))
  301. throw new Exception ("Failed in GetConsoleMode: " + Marshal.GetLastWin32Error ());
  302. bool cAsInput = ((mode & 1) == 0);
  303. if (cAsInput == value)
  304. return;
  305. if (value)
  306. mode &= ~1;
  307. else
  308. mode |= 1;
  309. if (!SetConsoleMode (inputHandle, mode))
  310. throw new Exception ("Failed in SetConsoleMode: " + Marshal.GetLastWin32Error ());
  311. }
  312. }
  313. public int WindowHeight {
  314. get {
  315. ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
  316. GetConsoleScreenBufferInfo (outputHandle, out info);
  317. return info.Window.Bottom - info.Window.Top + 1;
  318. }
  319. set { SetWindowSize (WindowWidth, value); }
  320. }
  321. public int WindowLeft {
  322. get {
  323. ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
  324. GetConsoleScreenBufferInfo (outputHandle, out info);
  325. return info.Window.Left;
  326. }
  327. set { SetWindowPosition (value, WindowTop); }
  328. }
  329. public int WindowTop {
  330. get {
  331. ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
  332. GetConsoleScreenBufferInfo (outputHandle, out info);
  333. return info.Window.Top;
  334. }
  335. set { SetWindowPosition (WindowLeft, value); }
  336. }
  337. public int WindowWidth {
  338. get {
  339. ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
  340. GetConsoleScreenBufferInfo (outputHandle, out info);
  341. return info.Window.Right - info.Window.Left + 1;
  342. }
  343. set { SetWindowSize (value, WindowHeight); }
  344. }
  345. public void Beep (int frequency, int duration)
  346. {
  347. _Beep (frequency, duration);
  348. }
  349. public void Clear ()
  350. {
  351. Coord coord = new Coord ();
  352. ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
  353. GetConsoleScreenBufferInfo (outputHandle, out info);
  354. int size = info.Size.X * info.Size.Y;
  355. int written;
  356. FillConsoleOutputCharacter (outputHandle, ' ', size, coord, out written);
  357. GetConsoleScreenBufferInfo (outputHandle, out info);
  358. FillConsoleOutputAttribute (outputHandle, info.Attribute, size, coord, out written);
  359. SetConsoleCursorPosition (outputHandle, coord);
  360. }
  361. public void MoveBufferArea (int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight,
  362. int targetLeft, int targetTop, Char sourceChar,
  363. ConsoleColor sourceForeColor, ConsoleColor sourceBackColor)
  364. {
  365. if (sourceForeColor < 0)
  366. throw new ArgumentException ("Cannot be less than 0.", "sourceForeColor");
  367. if (sourceBackColor < 0)
  368. throw new ArgumentException ("Cannot be less than 0.", "sourceBackColor");
  369. if (sourceWidth == 0 || sourceHeight == 0)
  370. return;
  371. ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
  372. GetConsoleScreenBufferInfo (outputHandle, out info);
  373. CharInfo [] buffer = new CharInfo [sourceWidth * sourceHeight];
  374. Coord bsize = new Coord (sourceWidth, sourceHeight);
  375. Coord bpos = new Coord (0, 0);
  376. SmallRect region = new SmallRect (sourceLeft, sourceTop, sourceLeft + sourceWidth - 1, sourceTop + sourceHeight - 1);
  377. fixed (void *ptr = &buffer [0]) {
  378. if (!ReadConsoleOutput (outputHandle, ptr, bsize, bpos, ref region))
  379. throw new ArgumentException (String.Empty, "Cannot read from the specified coordinates.");
  380. }
  381. int written;
  382. short attr = GetAttrForeground (0, sourceForeColor);
  383. attr = GetAttrBackground (attr, sourceBackColor);
  384. bpos = new Coord (sourceLeft, sourceTop);
  385. for (int i = 0; i < sourceHeight; i++, bpos.Y++) {
  386. FillConsoleOutputCharacter (outputHandle, sourceChar, sourceWidth, bpos, out written);
  387. FillConsoleOutputAttribute (outputHandle, attr, sourceWidth, bpos, out written);
  388. }
  389. bpos = new Coord (0, 0);
  390. region = new SmallRect (targetLeft, targetTop, targetLeft + sourceWidth - 1, targetTop + sourceHeight - 1);
  391. if (!WriteConsoleOutput (outputHandle, buffer, bsize, bpos, ref region))
  392. throw new ArgumentException (String.Empty, "Cannot write to the specified coordinates.");
  393. }
  394. public void Init ()
  395. {
  396. }
  397. public string ReadLine ()
  398. {
  399. StringBuilder builder = new StringBuilder ();
  400. bool exit = false;
  401. do {
  402. ConsoleKeyInfo key = ReadKey (false);
  403. char c = key.KeyChar;
  404. exit = (c == '\n');
  405. if (!exit)
  406. builder.Append (key.KeyChar);
  407. } while (!exit);
  408. return builder.ToString ();
  409. }
  410. public ConsoleKeyInfo ReadKey (bool intercept)
  411. {
  412. int eventsRead;
  413. InputRecord record = new InputRecord ();
  414. for (;;) {
  415. if (!ReadConsoleInput (inputHandle, out record, 1, out eventsRead))
  416. throw new InvalidOperationException ("Error in ReadConsoleInput " +
  417. Marshal.GetLastWin32Error ());
  418. if (record.KeyDown && record.EventType == 1 && !IsModifierKey (record.VirtualKeyCode))
  419. break;
  420. }
  421. // RIGHT_ALT_PRESSED 1
  422. // LEFT_ALT_PRESSED 2
  423. // RIGHT_CTRL_PRESSED 4
  424. // LEFT_CTRL_PRESSED 8
  425. // SHIFT_PRESSED 16
  426. bool alt = ((record.ControlKeyState & 3) != 0);
  427. bool ctrl = ((record.ControlKeyState & 12) != 0);
  428. bool shift = ((record.ControlKeyState & 16) != 0);
  429. return new ConsoleKeyInfo (record.Character, (ConsoleKey) record.VirtualKeyCode, shift, alt, ctrl);
  430. }
  431. public void ResetColor ()
  432. {
  433. SetConsoleTextAttribute (outputHandle, defaultAttribute);
  434. }
  435. public void SetBufferSize (int width, int height)
  436. {
  437. ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
  438. GetConsoleScreenBufferInfo (outputHandle, out info);
  439. if (width - 1 > info.Window.Right)
  440. throw new ArgumentOutOfRangeException ("width");
  441. if (height - 1 > info.Window.Bottom)
  442. throw new ArgumentOutOfRangeException ("height");
  443. Coord coord = new Coord (width, height);
  444. if (!SetConsoleScreenBufferSize (outputHandle, coord))
  445. throw new ArgumentOutOfRangeException ("height/width", "Cannot be smaller than the window size.");
  446. }
  447. public void SetCursorPosition (int left, int top)
  448. {
  449. Coord coord = new Coord (left, top);
  450. SetConsoleCursorPosition (outputHandle, coord);
  451. }
  452. public void SetWindowPosition (int left, int top)
  453. {
  454. ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
  455. GetConsoleScreenBufferInfo (outputHandle, out info);
  456. SmallRect rect = info.Window;
  457. rect.Left = (short) left;
  458. rect.Top = (short) top;
  459. if (!SetConsoleWindowInfo (outputHandle, true, ref rect))
  460. throw new ArgumentOutOfRangeException ("left/top", "Windows error " + Marshal.GetLastWin32Error ());
  461. }
  462. public void SetWindowSize (int width, int height)
  463. {
  464. ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo ();
  465. GetConsoleScreenBufferInfo (outputHandle, out info);
  466. SmallRect rect = info.Window;
  467. rect.Right = (short) (rect.Left + width - 1);
  468. rect.Bottom = (short) (rect.Top + height - 1);
  469. if (!SetConsoleWindowInfo (outputHandle, true, ref rect))
  470. throw new ArgumentOutOfRangeException ("left/top", "Windows error " + Marshal.GetLastWin32Error ());
  471. }
  472. private bool IsModifierKey(short virtualKeyCode)
  473. {
  474. // 0x10 through 0x14 is shift/control/alt/pause/capslock
  475. // 0x2C is print screen, 0x90 is numlock, 0x91 is scroll lock
  476. switch (virtualKeyCode) {
  477. case 0x10:
  478. case 0x11:
  479. case 0x12:
  480. case 0x13:
  481. case 0x14:
  482. return true;
  483. case 0x2C:
  484. case 0x90:
  485. case 0x91:
  486. return true;
  487. default:
  488. return false;
  489. }
  490. }
  491. //
  492. // Imports
  493. //
  494. [DllImport ("kernel32.dll", EntryPoint="GetStdHandle", SetLastError=true, CharSet=CharSet.Unicode)]
  495. extern static IntPtr GetStdHandle (Handles handle);
  496. [DllImport ("kernel32.dll", EntryPoint="Beep", SetLastError=true, CharSet=CharSet.Unicode)]
  497. extern static void _Beep (int frequency, int duration);
  498. [DllImport ("kernel32.dll", EntryPoint="GetConsoleScreenBufferInfo", SetLastError=true, CharSet=CharSet.Unicode)]
  499. extern static bool GetConsoleScreenBufferInfo (IntPtr handle, out ConsoleScreenBufferInfo info);
  500. [DllImport ("kernel32.dll", EntryPoint="FillConsoleOutputCharacter", SetLastError=true, CharSet=CharSet.Unicode)]
  501. extern static bool FillConsoleOutputCharacter (IntPtr handle, char c, int size, Coord coord, out int written);
  502. [DllImport ("kernel32.dll", EntryPoint="FillConsoleOutputAttribute", SetLastError=true, CharSet=CharSet.Unicode)]
  503. extern static bool FillConsoleOutputAttribute (IntPtr handle, short c, int size, Coord coord, out int written);
  504. [DllImport ("kernel32.dll", EntryPoint="SetConsoleCursorPosition", SetLastError=true, CharSet=CharSet.Unicode)]
  505. extern static bool SetConsoleCursorPosition (IntPtr handle, Coord coord);
  506. [DllImport ("kernel32.dll", EntryPoint="SetConsoleTextAttribute", SetLastError=true, CharSet=CharSet.Unicode)]
  507. extern static bool SetConsoleTextAttribute (IntPtr handle, short attribute);
  508. [DllImport ("kernel32.dll", EntryPoint="SetConsoleScreenBufferSize", SetLastError=true, CharSet=CharSet.Unicode)]
  509. extern static bool SetConsoleScreenBufferSize (IntPtr handle, Coord newSize);
  510. [DllImport ("kernel32.dll", EntryPoint="SetConsoleWindowInfo", SetLastError=true, CharSet=CharSet.Unicode)]
  511. extern static bool SetConsoleWindowInfo (IntPtr handle, bool absolute, ref SmallRect rect);
  512. [DllImport ("kernel32.dll", EntryPoint="GetConsoleTitle", SetLastError=true, CharSet=CharSet.Unicode)]
  513. extern static int GetConsoleTitle (StringBuilder sb, int size);
  514. [DllImport ("kernel32.dll", EntryPoint="SetConsoleTitle", SetLastError=true, CharSet=CharSet.Unicode)]
  515. extern static bool SetConsoleTitle (string title);
  516. [DllImport ("kernel32.dll", EntryPoint="GetConsoleCursorInfo", SetLastError=true, CharSet=CharSet.Unicode)]
  517. extern static bool GetConsoleCursorInfo (IntPtr handle, out ConsoleCursorInfo info);
  518. [DllImport ("kernel32.dll", EntryPoint="SetConsoleCursorInfo", SetLastError=true, CharSet=CharSet.Unicode)]
  519. extern static bool SetConsoleCursorInfo (IntPtr handle, ref ConsoleCursorInfo info);
  520. [DllImport ("user32.dll", EntryPoint="GetKeyState", SetLastError=true, CharSet=CharSet.Unicode)]
  521. extern static short GetKeyState (int virtKey);
  522. [DllImport ("kernel32.dll", EntryPoint="GetConsoleMode", SetLastError=true, CharSet=CharSet.Unicode)]
  523. extern static bool GetConsoleMode (IntPtr handle, out int mode);
  524. [DllImport ("kernel32.dll", EntryPoint="SetConsoleMode", SetLastError=true, CharSet=CharSet.Unicode)]
  525. extern static bool SetConsoleMode (IntPtr handle, int mode);
  526. [DllImport ("kernel32.dll", EntryPoint="PeekConsoleInput", SetLastError=true, CharSet=CharSet.Unicode)]
  527. extern static bool PeekConsoleInput (IntPtr handle, out InputRecord record, int length, out int eventsRead);
  528. [DllImport ("kernel32.dll", EntryPoint="ReadConsoleInput", SetLastError=true, CharSet=CharSet.Unicode)]
  529. extern static bool ReadConsoleInput (IntPtr handle, out InputRecord record, int length, out int nread);
  530. [DllImport ("kernel32.dll", EntryPoint="GetLargestConsoleWindowSize", SetLastError=true, CharSet=CharSet.Unicode)]
  531. extern static Coord GetLargestConsoleWindowSize (IntPtr handle);
  532. [DllImport ("kernel32.dll", EntryPoint="ReadConsoleOutput", SetLastError=true, CharSet=CharSet.Unicode)]
  533. extern static bool ReadConsoleOutput (IntPtr handle, void *buffer, Coord bsize, Coord bpos, ref SmallRect region);
  534. [DllImport ("kernel32.dll", EntryPoint="WriteConsoleOutput", SetLastError=true, CharSet=CharSet.Unicode)]
  535. extern static bool WriteConsoleOutput (IntPtr handle, CharInfo [] buffer, Coord bsize, Coord bpos, ref SmallRect region);
  536. }
  537. }
  538. #endif