KeyboardTests.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. using Xunit.Abstractions;
  2. namespace Terminal.Gui.ApplicationTests;
  3. /// <summary>
  4. /// Application tests for keyboard support.
  5. /// </summary>
  6. public class KeyboardTests
  7. {
  8. public KeyboardTests (ITestOutputHelper output)
  9. {
  10. _output = output;
  11. #if DEBUG_IDISPOSABLE
  12. Responder.Instances.Clear ();
  13. RunState.Instances.Clear ();
  14. #endif
  15. }
  16. private readonly ITestOutputHelper _output;
  17. private object _timeoutLock;
  18. [Fact (Skip = "No longer valid test.")]
  19. [AutoInitShutdown]
  20. public void EnsuresTopOnFront_CanFocus_False_By_Keyboard ()
  21. {
  22. Toplevel top = new ();
  23. var win = new Window
  24. {
  25. Title = "win",
  26. X = 0,
  27. Y = 0,
  28. Width = 20,
  29. Height = 10
  30. };
  31. var tf = new TextField { Width = 10 };
  32. win.Add (tf);
  33. var win2 = new Window
  34. {
  35. Title = "win2",
  36. X = 22,
  37. Y = 0,
  38. Width = 20,
  39. Height = 10
  40. };
  41. var tf2 = new TextField { Width = 10 };
  42. win2.Add (tf2);
  43. top.Add (win, win2);
  44. Application.Begin (top);
  45. Assert.True (win.CanFocus);
  46. Assert.True (win.HasFocus);
  47. Assert.True (win2.CanFocus);
  48. Assert.False (win2.HasFocus);
  49. Assert.Equal ("win", ((Window)top.Subviews [^1]).Title);
  50. win.CanFocus = false;
  51. Assert.False (win.CanFocus);
  52. Assert.False (win.HasFocus);
  53. Assert.True (win2.CanFocus);
  54. Assert.True (win2.HasFocus);
  55. Assert.Equal ("win2", ((Window)top.Subviews [^1]).Title);
  56. Application.RaiseKeyDownEvent (Key.F6);
  57. Assert.True (win2.CanFocus);
  58. Assert.False (win.HasFocus);
  59. Assert.True (win2.CanFocus);
  60. Assert.True (win2.HasFocus);
  61. Assert.Equal ("win2", ((Window)top.Subviews [^1]).Title);
  62. Application.RaiseKeyDownEvent (Key.F6);
  63. Assert.False (win.CanFocus);
  64. Assert.False (win.HasFocus);
  65. Assert.True (win2.CanFocus);
  66. Assert.True (win2.HasFocus);
  67. Assert.Equal ("win2", ((Window)top.Subviews [^1]).Title);
  68. top.Dispose ();
  69. }
  70. [Fact (Skip = "No longer valid test.")]
  71. [AutoInitShutdown]
  72. public void EnsuresTopOnFront_CanFocus_True_By_Keyboard ()
  73. {
  74. Toplevel top = new ();
  75. var win = new Window
  76. {
  77. Title = "win",
  78. X = 0,
  79. Y = 0,
  80. Width = 20,
  81. Height = 10
  82. };
  83. var tf = new TextField { Width = 10 };
  84. win.Add (tf);
  85. var win2 = new Window
  86. {
  87. Title = "win2",
  88. X = 22,
  89. Y = 0,
  90. Width = 20,
  91. Height = 10
  92. };
  93. var tf2 = new TextField { Width = 10 };
  94. win2.Add (tf2);
  95. top.Add (win, win2);
  96. Application.Begin (top);
  97. Assert.True (win.CanFocus);
  98. Assert.True (win.HasFocus);
  99. Assert.True (win2.CanFocus);
  100. Assert.False (win2.HasFocus);
  101. Assert.Equal ("win", ((Window)top.Subviews [^1]).Title);
  102. Application.RaiseKeyDownEvent (Key.F6);
  103. Assert.True (win.CanFocus);
  104. Assert.False (win.HasFocus);
  105. Assert.True (win2.CanFocus);
  106. Assert.True (win2.HasFocus);
  107. Assert.Equal ("win2", ((Window)top.Subviews [^1]).Title);
  108. Application.RaiseKeyDownEvent (Key.F6);
  109. Assert.True (win.CanFocus);
  110. Assert.True (win.HasFocus);
  111. Assert.True (win2.CanFocus);
  112. Assert.False (win2.HasFocus);
  113. Assert.Equal ("win", ((Window)top.Subviews [^1]).Title);
  114. top.Dispose ();
  115. }
  116. [Fact]
  117. [AutoInitShutdown]
  118. public void KeyBinding_Application_KeyBindings_Add_Adds ()
  119. {
  120. Application.KeyBindings.Add (Key.A, KeyBindingScope.Application, Command.Accept);
  121. Application.KeyBindings.Add (Key.B, KeyBindingScope.Application, Command.Accept);
  122. Assert.True (Application.KeyBindings.TryGet (Key.A, out KeyBinding binding));
  123. Assert.Null (binding.BoundView);
  124. Assert.True (Application.KeyBindings.TryGet (Key.B, out binding));
  125. Assert.Null (binding.BoundView);
  126. }
  127. [Fact]
  128. [AutoInitShutdown]
  129. public void KeyBinding_Application_RemoveKeyBinding_Removes ()
  130. {
  131. Application.KeyBindings.Add (Key.A, KeyBindingScope.Application, Command.Accept);
  132. Assert.True (Application.KeyBindings.TryGet (Key.A, out _));
  133. Application.KeyBindings.Remove (Key.A);
  134. Assert.False (Application.KeyBindings.TryGet (Key.A, out _));
  135. }
  136. [Fact]
  137. [AutoInitShutdown]
  138. public void KeyBinding_OnKeyDown ()
  139. {
  140. var view = new ScopedKeyBindingView ();
  141. var keyWasHandled = false;
  142. view.KeyDownNotHandled += (s, e) => keyWasHandled = true;
  143. var top = new Toplevel ();
  144. top.Add (view);
  145. Application.Begin (top);
  146. Application.RaiseKeyDownEvent (Key.A);
  147. Assert.False (keyWasHandled);
  148. Assert.True (view.ApplicationCommand);
  149. keyWasHandled = false;
  150. view.ApplicationCommand = false;
  151. Application.KeyBindings.Remove (KeyCode.A);
  152. Application.RaiseKeyDownEvent (Key.A); // old
  153. Assert.False (keyWasHandled);
  154. Assert.False (view.ApplicationCommand);
  155. Application.KeyBindings.Add (Key.A.WithCtrl, view, Command.Save);
  156. Application.RaiseKeyDownEvent (Key.A); // old
  157. Assert.False (keyWasHandled);
  158. Assert.False (view.ApplicationCommand);
  159. Application.RaiseKeyDownEvent (Key.A.WithCtrl); // new
  160. Assert.False (keyWasHandled);
  161. Assert.True (view.ApplicationCommand);
  162. keyWasHandled = false;
  163. Application.RaiseKeyDownEvent (Key.H);
  164. Assert.False (keyWasHandled);
  165. keyWasHandled = false;
  166. Assert.False (view.HasFocus);
  167. Application.RaiseKeyDownEvent (Key.F);
  168. Assert.False (keyWasHandled);
  169. Assert.True (view.ApplicationCommand);
  170. Assert.True (view.HotKeyCommand);
  171. Assert.False (view.FocusedCommand);
  172. top.Dispose ();
  173. }
  174. [Fact]
  175. [AutoInitShutdown]
  176. public void KeyBinding_OnKeyDown_Negative ()
  177. {
  178. var view = new ScopedKeyBindingView ();
  179. var keyWasHandled = false;
  180. view.KeyDownNotHandled += (s, e) => keyWasHandled = true;
  181. var top = new Toplevel ();
  182. top.Add (view);
  183. Application.Begin (top);
  184. Application.RaiseKeyDownEvent (Key.A.WithCtrl);
  185. Assert.False (keyWasHandled);
  186. Assert.False (view.ApplicationCommand);
  187. Assert.False (view.HotKeyCommand);
  188. Assert.False (view.FocusedCommand);
  189. keyWasHandled = false;
  190. Assert.False (view.HasFocus);
  191. Application.RaiseKeyDownEvent (Key.Z);
  192. Assert.False (keyWasHandled);
  193. Assert.False (view.ApplicationCommand);
  194. Assert.False (view.HotKeyCommand);
  195. Assert.False (view.FocusedCommand);
  196. top.Dispose ();
  197. }
  198. [Fact]
  199. [AutoInitShutdown]
  200. public void KeyBinding_View_KeyBindings_Add_Adds ()
  201. {
  202. View view1 = new ();
  203. Application.KeyBindings.Add (Key.A, view1, Command.Accept);
  204. View view2 = new ();
  205. Application.KeyBindings.Add (Key.B, view2, Command.Accept);
  206. Assert.True (Application.KeyBindings.TryGet (Key.A, out KeyBinding binding));
  207. Assert.Equal (view1, binding.BoundView);
  208. Assert.True (Application.KeyBindings.TryGet (Key.B, out binding));
  209. Assert.Equal (view2, binding.BoundView);
  210. }
  211. [Fact]
  212. [AutoInitShutdown]
  213. public void KeyBinding_View_KeyBindings_RemoveKeyBinding_Removes ()
  214. {
  215. View view1 = new ();
  216. Application.KeyBindings.Add (Key.A, view1, Command.Accept);
  217. View view2 = new ();
  218. Application.KeyBindings.Add (Key.B, view1, Command.Accept);
  219. Application.KeyBindings.Remove (Key.A, view1);
  220. Assert.False (Application.KeyBindings.TryGet (Key.A, out _));
  221. }
  222. [Fact]
  223. public void KeyUp_Event ()
  224. {
  225. Application.Init (new FakeDriver ());
  226. // Setup some fake keypresses (This)
  227. var input = "Tests";
  228. Key originalQuitKey = Application.QuitKey;
  229. Application.QuitKey = Key.Q.WithCtrl;
  230. // Put a control-q in at the end
  231. FakeConsole.MockKeyPresses.Push (new ('Q', ConsoleKey.Q, false, false, true));
  232. foreach (char c in input.Reverse ())
  233. {
  234. if (char.IsLetter (c))
  235. {
  236. FakeConsole.MockKeyPresses.Push (
  237. new (
  238. c,
  239. (ConsoleKey)char.ToUpper (c),
  240. char.IsUpper (c),
  241. false,
  242. false
  243. )
  244. );
  245. }
  246. else
  247. {
  248. FakeConsole.MockKeyPresses.Push (
  249. new (
  250. c,
  251. (ConsoleKey)c,
  252. false,
  253. false,
  254. false
  255. )
  256. );
  257. }
  258. }
  259. int stackSize = FakeConsole.MockKeyPresses.Count;
  260. var iterations = 0;
  261. Application.Iteration += (s, a) =>
  262. {
  263. iterations++;
  264. // Stop if we run out of control...
  265. if (iterations > 10)
  266. {
  267. Application.RequestStop ();
  268. }
  269. };
  270. var keyUps = 0;
  271. var output = string.Empty;
  272. var top = new Toplevel ();
  273. top.KeyUp += (sender, args) =>
  274. {
  275. if (args.KeyCode != (KeyCode.CtrlMask | KeyCode.Q))
  276. {
  277. output += args.AsRune;
  278. }
  279. keyUps++;
  280. };
  281. Application.Run (top);
  282. Application.QuitKey = originalQuitKey;
  283. // Input string should match output
  284. Assert.Equal (input, output);
  285. // # of key up events should match stack size
  286. //Assert.Equal (stackSize, keyUps);
  287. // We can't use numbers variables on the left side of an Assert.Equal/NotEqual,
  288. // it must be literal (Linux only).
  289. Assert.Equal (6, keyUps);
  290. // # of key up events should match # of iterations
  291. Assert.Equal (stackSize, iterations);
  292. top.Dispose ();
  293. Application.Shutdown ();
  294. Assert.Null (Application.Top);
  295. Assert.Null (Application.MainLoop);
  296. Assert.Null (Application.Driver);
  297. }
  298. [Fact]
  299. public void NextTabGroupKey_Moves_Focus_To_TabStop_In_Next_TabGroup ()
  300. {
  301. // Arrange
  302. Application.Navigation = new ();
  303. var top = new Toplevel ();
  304. var view1 = new View
  305. {
  306. Id = "view1",
  307. CanFocus = true,
  308. TabStop = TabBehavior.TabGroup
  309. };
  310. var subView1 = new View
  311. {
  312. Id = "subView1",
  313. CanFocus = true,
  314. TabStop = TabBehavior.TabStop
  315. };
  316. view1.Add (subView1);
  317. var view2 = new View
  318. {
  319. Id = "view2",
  320. CanFocus = true,
  321. TabStop = TabBehavior.TabGroup
  322. };
  323. var subView2 = new View
  324. {
  325. Id = "subView2",
  326. CanFocus = true,
  327. TabStop = TabBehavior.TabStop
  328. };
  329. view2.Add (subView2);
  330. top.Add (view1, view2);
  331. Application.Top = top;
  332. view1.SetFocus ();
  333. Assert.True (view1.HasFocus);
  334. Assert.True (subView1.HasFocus);
  335. // Act
  336. Application.RaiseKeyDownEvent (Application.NextTabGroupKey);
  337. // Assert
  338. Assert.True (view2.HasFocus);
  339. Assert.True (subView2.HasFocus);
  340. top.Dispose ();
  341. Application.Navigation = null;
  342. }
  343. [Fact]
  344. public void NextTabGroupKey_PrevTabGroupKey_Tests ()
  345. {
  346. Application.Init (new FakeDriver ());
  347. Toplevel top = new (); // TabGroup
  348. var w1 = new Window (); // TabGroup
  349. var v1 = new TextField (); // TabStop
  350. var v2 = new TextView (); // TabStop
  351. w1.Add (v1, v2);
  352. var w2 = new Window (); // TabGroup
  353. var v3 = new CheckBox (); // TabStop
  354. var v4 = new Button (); // TabStop
  355. w2.Add (v3, v4);
  356. top.Add (w1, w2);
  357. Application.Iteration += (s, a) =>
  358. {
  359. Assert.True (v1.HasFocus);
  360. // Across TabGroups
  361. Application.RaiseKeyDownEvent (Key.F6);
  362. Assert.True (v3.HasFocus);
  363. Application.RaiseKeyDownEvent (Key.F6);
  364. Assert.True (v1.HasFocus);
  365. Application.RaiseKeyDownEvent (Key.F6.WithShift);
  366. Assert.True (v3.HasFocus);
  367. Application.RaiseKeyDownEvent (Key.F6.WithShift);
  368. Assert.True (v1.HasFocus);
  369. // Restore?
  370. Application.RaiseKeyDownEvent (Key.Tab);
  371. Assert.True (v2.HasFocus);
  372. Application.RaiseKeyDownEvent (Key.F6);
  373. Assert.True (v3.HasFocus);
  374. Application.RaiseKeyDownEvent (Key.F6);
  375. Assert.True (v1.HasFocus);
  376. Application.RequestStop ();
  377. };
  378. Application.Run (top);
  379. // Replacing the defaults keys to avoid errors on others unit tests that are using it.
  380. Application.NextTabGroupKey = Key.PageDown.WithCtrl;
  381. Application.PrevTabGroupKey = Key.PageUp.WithCtrl;
  382. Application.QuitKey = Key.Q.WithCtrl;
  383. Assert.Equal (KeyCode.PageDown | KeyCode.CtrlMask, Application.NextTabGroupKey.KeyCode);
  384. Assert.Equal (KeyCode.PageUp | KeyCode.CtrlMask, Application.PrevTabGroupKey.KeyCode);
  385. Assert.Equal (KeyCode.Q | KeyCode.CtrlMask, Application.QuitKey.KeyCode);
  386. top.Dispose ();
  387. // Shutdown must be called to safely clean up Application if Init has been called
  388. Application.Shutdown ();
  389. }
  390. [Fact]
  391. public void NextTabKey_Moves_Focus_To_Next_TabStop ()
  392. {
  393. // Arrange
  394. Application.Navigation = new ();
  395. var top = new Toplevel ();
  396. var view1 = new View { Id = "view1", CanFocus = true };
  397. var view2 = new View { Id = "view2", CanFocus = true };
  398. top.Add (view1, view2);
  399. Application.Top = top;
  400. view1.SetFocus ();
  401. // Act
  402. Application.RaiseKeyDownEvent (Application.NextTabKey);
  403. // Assert
  404. Assert.True (view2.HasFocus);
  405. top.Dispose ();
  406. Application.Navigation = null;
  407. }
  408. [Fact]
  409. public void PrevTabGroupKey_Moves_Focus_To_TabStop_In_Prev_TabGroup ()
  410. {
  411. // Arrange
  412. Application.Navigation = new ();
  413. var top = new Toplevel ();
  414. var view1 = new View
  415. {
  416. Id = "view1",
  417. CanFocus = true,
  418. TabStop = TabBehavior.TabGroup
  419. };
  420. var subView1 = new View
  421. {
  422. Id = "subView1",
  423. CanFocus = true,
  424. TabStop = TabBehavior.TabStop
  425. };
  426. view1.Add (subView1);
  427. var view2 = new View
  428. {
  429. Id = "view2",
  430. CanFocus = true,
  431. TabStop = TabBehavior.TabGroup
  432. };
  433. var subView2 = new View
  434. {
  435. Id = "subView2",
  436. CanFocus = true,
  437. TabStop = TabBehavior.TabStop
  438. };
  439. view2.Add (subView2);
  440. top.Add (view1, view2);
  441. Application.Top = top;
  442. view1.SetFocus ();
  443. Assert.True (view1.HasFocus);
  444. Assert.True (subView1.HasFocus);
  445. // Act
  446. Application.RaiseKeyDownEvent (Application.PrevTabGroupKey);
  447. // Assert
  448. Assert.True (view2.HasFocus);
  449. Assert.True (subView2.HasFocus);
  450. top.Dispose ();
  451. Application.Navigation = null;
  452. }
  453. [Fact]
  454. public void PrevTabKey_Moves_Focus_To_Prev_TabStop ()
  455. {
  456. // Arrange
  457. Application.Navigation = new ();
  458. var top = new Toplevel ();
  459. var view1 = new View { Id = "view1", CanFocus = true };
  460. var view2 = new View { Id = "view2", CanFocus = true };
  461. top.Add (view1, view2);
  462. Application.Top = top;
  463. view1.SetFocus ();
  464. // Act
  465. Application.RaiseKeyDownEvent (Application.NextTabKey);
  466. // Assert
  467. Assert.True (view2.HasFocus);
  468. top.Dispose ();
  469. Application.Navigation = null;
  470. }
  471. [Fact]
  472. public void QuitKey_Default_Is_Esc ()
  473. {
  474. Application.ResetState (true);
  475. // Before Init
  476. Assert.Equal (Key.Esc, Application.QuitKey);
  477. Application.Init (new FakeDriver ());
  478. // After Init
  479. Assert.Equal (Key.Esc, Application.QuitKey);
  480. Application.Shutdown ();
  481. }
  482. [Fact]
  483. [AutoInitShutdown]
  484. public void QuitKey_Getter_Setter ()
  485. {
  486. Toplevel top = new ();
  487. var isQuiting = false;
  488. top.Closing += (s, e) =>
  489. {
  490. isQuiting = true;
  491. e.Cancel = true;
  492. };
  493. Application.Begin (top);
  494. top.Running = true;
  495. Key prevKey = Application.QuitKey;
  496. Application.RaiseKeyDownEvent (Application.QuitKey);
  497. Assert.True (isQuiting);
  498. isQuiting = false;
  499. Application.RaiseKeyDownEvent (Application.QuitKey);
  500. Assert.True (isQuiting);
  501. isQuiting = false;
  502. Application.QuitKey = Key.C.WithCtrl;
  503. Application.RaiseKeyDownEvent (prevKey); // Should not quit
  504. Assert.False (isQuiting);
  505. Application.RaiseKeyDownEvent (Key.Q.WithCtrl); // Should not quit
  506. Assert.False (isQuiting);
  507. Application.RaiseKeyDownEvent (Application.QuitKey);
  508. Assert.True (isQuiting);
  509. // Reset the QuitKey to avoid throws errors on another tests
  510. Application.QuitKey = prevKey;
  511. top.Dispose ();
  512. }
  513. [Fact]
  514. public void QuitKey_Quits ()
  515. {
  516. Assert.Null (_timeoutLock);
  517. _timeoutLock = new ();
  518. uint abortTime = 500;
  519. var initialized = false;
  520. var iteration = 0;
  521. var shutdown = false;
  522. object timeout = null;
  523. Application.InitializedChanged += OnApplicationOnInitializedChanged;
  524. Application.Init (new FakeDriver ());
  525. Assert.True (initialized);
  526. Assert.False (shutdown);
  527. _output.WriteLine ("Application.Run<Toplevel> ().Dispose ()..");
  528. Application.Run<Toplevel> ().Dispose ();
  529. _output.WriteLine ("Back from Application.Run<Toplevel> ().Dispose ()");
  530. Assert.True (initialized);
  531. Assert.False (shutdown);
  532. Assert.Equal (1, iteration);
  533. Application.Shutdown ();
  534. Application.InitializedChanged -= OnApplicationOnInitializedChanged;
  535. lock (_timeoutLock)
  536. {
  537. if (timeout is { })
  538. {
  539. Application.RemoveTimeout (timeout);
  540. timeout = null;
  541. }
  542. }
  543. Assert.True (initialized);
  544. Assert.True (shutdown);
  545. #if DEBUG_IDISPOSABLE
  546. Assert.Empty (Responder.Instances);
  547. #endif
  548. lock (_timeoutLock)
  549. {
  550. _timeoutLock = null;
  551. }
  552. return;
  553. void OnApplicationOnInitializedChanged (object s, EventArgs<bool> a)
  554. {
  555. _output.WriteLine ("OnApplicationOnInitializedChanged: {0}", a.CurrentValue);
  556. if (a.CurrentValue)
  557. {
  558. Application.Iteration += OnApplicationOnIteration;
  559. initialized = true;
  560. lock (_timeoutLock)
  561. {
  562. timeout = Application.AddTimeout (TimeSpan.FromMilliseconds (abortTime), ForceCloseCallback);
  563. }
  564. }
  565. else
  566. {
  567. Application.Iteration -= OnApplicationOnIteration;
  568. shutdown = true;
  569. }
  570. }
  571. bool ForceCloseCallback ()
  572. {
  573. lock (_timeoutLock)
  574. {
  575. _output.WriteLine ($"ForceCloseCallback. iteration: {iteration}");
  576. if (timeout is { })
  577. {
  578. timeout = null;
  579. }
  580. }
  581. Application.ResetState (true);
  582. Assert.Fail ($"Failed to Quit with {Application.QuitKey} after {abortTime}ms. Force quit.");
  583. return false;
  584. }
  585. void OnApplicationOnIteration (object s, IterationEventArgs a)
  586. {
  587. _output.WriteLine ("Iteration: {0}", iteration);
  588. iteration++;
  589. Assert.True (iteration < 2, "Too many iterations, something is wrong.");
  590. if (Application.IsInitialized)
  591. {
  592. _output.WriteLine (" Pressing QuitKey");
  593. Application.RaiseKeyDownEvent (Application.QuitKey);
  594. }
  595. }
  596. }
  597. // Test View for testing Application key Bindings
  598. public class ScopedKeyBindingView : View
  599. {
  600. public ScopedKeyBindingView ()
  601. {
  602. AddCommand (Command.Save, () => ApplicationCommand = true);
  603. AddCommand (Command.HotKey, () => HotKeyCommand = true);
  604. AddCommand (Command.Left, () => FocusedCommand = true);
  605. Application.KeyBindings.Add (Key.A, this, Command.Save);
  606. HotKey = KeyCode.H;
  607. KeyBindings.Add (Key.F, Command.Left);
  608. }
  609. public bool ApplicationCommand { get; set; }
  610. public bool FocusedCommand { get; set; }
  611. public bool HotKeyCommand { get; set; }
  612. }
  613. }