Defining a Quit Key #2628
-
I am struggling to define an alternate QuitKey. First off, it seems that if I change the QuitKey in one application, it applies to the next application if I don't specify anything. Is this correct, or am I missing a step after running I'm pretty sure that setting a single key works, [Terminal.Gui.Application]::QuitKey = [Terminal.Gui.Key]::F12 But I can't find a consistent way I've been variations on this in a PowerShell script. [Terminal.Gui.Application]::QuitKey = ('q' -as [Terminal.Gui.Key])+[Terminal.Gui.Key]::CtrlMask I assume this means I can type Ctrl+q to quit. But it fails. Using Alt+q will work. I even tried using the value. [Terminal.Gui.Application]::QuitKey =1073741937 I have to use Alt+q to quit. I'm running in PowerShell 7 in Windows Terminal. Is this a bug, or is there something I'm not understanding about how to define a quit key? I have not had much luck finding documentation on this. |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 1 reply
-
Hi thanks for reaching out. Application.QuitKey is a static field. That means changing it in one place changes it permanently across your whole program until you change it again. But you are free to read and write to the variable as many times as you want. You can run an application with After the user quits you should call I'm afraid I can't help with the powershell syntax but here is how you would do it in c#. Its important when you want to combine a modifier and a key to use the 'bitwise or' to combine flags. In C# this is For example: using myproj;
using Terminal.Gui;
Application.Init();
// Store the default key for later if you want to reset the default
var origQuitKey = Application.QuitKey;
// Use a custom quit key for app 1
Application.QuitKey = Key.AltMask | Key.Q;
var w1 = new Window{Title = "App1 (Alt+Q to quit)"};
Application.Init();
Application.Run(w1);
Application.Shutdown();
// Use a different custom quit key for app 2
Application.QuitKey = origQuit;
var w2 = new Window{Title = "App2 (Ctrl+Q to quit)"};
Application.Init();
Application.Run(w2);
Application.Shutdown(); |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Interesting maybe there is a problem with how powershell sees classes that have fields differing only in case. You might be able to avoid the problem with casting from the char. I don't know how powershell would phrase this but in c# it would be: Application.QuitKey = ((Key)(int)'Q') | Key.AltMask;
Or
Application.QuitKey = ((Key)(int)'q') | Key.AltMask; |
Beta Was this translation helpful? Give feedback.
-
I found the best solution is to define a quit key with an integer value. |
Beta Was this translation helpful? Give feedback.
-
I finally realized the | character was representing a -BOR operation. This all makes more sense to me now. $MenuItem0.ShortCut = [int][Terminal.Gui.Key]"CtrlMask" -bor [int][char]"X" |
Beta Was this translation helpful? Give feedback.
-
Thanks for taking the time to come back and post when you realized this and
sharing the powershell language for this. It may help future users!
…On Mon, 25 Sep 2023, 16:43 BDisp, ***@***.***> wrote:
Yes is true it is using a bitwise OR operation.
—
Reply to this email directly, view it on GitHub
<#2628 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AHO3C5B5IZUN5VU6AHI6W4LX4GRAPANCNFSM6AAAAAAX74GU24>
.
You are receiving this because you commented.Message ID: <gui-cs/Terminal.
***@***.***>
|
Beta Was this translation helpful? Give feedback.
I finally realized the | character was representing a -BOR operation. This all makes more sense to me now.