Replies: 4 comments
-
I could be mistaken but I don't think it is possible to detect a held down key via the console. Holding down a key typically sends the key multiple times to the console input layer while pressing shift/control alone sends nothing at all (I think). If this is the case any you can't live without knowing key states then you might be able to implement your own platform specific keyboard monitoring and suppress key events in gui.cs with: Application.RootKeyEvent += (k) => true; For example in Windows: Here's a promising looking thread on how to directly detect key states in linux: |
Beta Was this translation helpful? Give feedback.
-
The behavior is different across platforms, terminals, and drivers. What platform, terminal, & driver do you care most about? If it's Windows, you have SOME chance of probably doing what you want. I have a feeling you're barking up the wrong tree with the UI you are trying to enable by doing this. Can you describe what that is? |
Beta Was this translation helpful? Give feedback.
-
Thanks for your help! I need to display status informations depending on what modifier key/keys is holding down. Mostly on Windows, but it would be nice as cross-platform. |
Beta Was this translation helpful? Give feedback.
-
So this works pretty well. It works with the constraint that what you see is This approach works with modifier+letter or letter alone but won't work for modifier key alone (which I don't think is possible without a lot of work and OS specific code). using Terminal.Gui;
DateTime ctrlS = DateTime.MinValue;
DateTime ctrlA = DateTime.MinValue;
var label = new Label() { X = 0, Y = 0, Width = Dim.Fill(), Height = 1 };
Application.Init();
Application.Top.Add(label);
Application.RootKeyEvent += RootKeyEvent;
Application.MainLoop.AddTimeout(TimeSpan.FromMilliseconds(100), UpdateLabel);
Application.Run();
Application.Shutdown();
bool RootKeyEvent(KeyEvent arg)
{
if (arg.Key.HasFlag(Key.CtrlMask) && arg.Key.HasFlag(Key.S))
{
ctrlS = DateTime.Now; // we saw S recently
ctrlA = DateTime.MinValue; //clear A
return true; // swallow keystroke
}
if (arg.Key.HasFlag(Key.CtrlMask) && arg.Key.HasFlag(Key.A))
{
ctrlA = DateTime.Now; // we saw A recently
ctrlS = DateTime.MinValue; // clear S
return true; // swallow keystroke
}
return false;
}
bool UpdateLabel(MainLoop arg)
{
// if its been a while since we last saw Ctrl+S
if (DateTime.Now.Subtract(ctrlS) < TimeSpan.FromMilliseconds(750))
{
label.Text = "Info about S";
}
else
if (DateTime.Now.Subtract(ctrlA) < TimeSpan.FromMilliseconds(750))
{
label.Text = "Info about A";
}
else
{
label.Text = "";
}
return true;
} |
Beta Was this translation helpful? Give feedback.
-
Hi!
How to handle when multiple keys pressed, and released only one?
Ex.: Press Ctrl+S, and release the S key.
After released the S key, i need info at KeyDown event about Ctrl is pressed, but KeyDown event doesn't fired and KeyUp event shown Ctrl is up (but not). Later, when i released the Ctrl key, the KeyUp event fired with CtrlMask.
Beta Was this translation helpful? Give feedback.
All reactions