Re: Disable keyboard shortscuts when in gui elements
Posted: 16 Jun 2020, 14:04
Correct me if I'm wrong, but doing this has the same problem as using the KeyTrigger, because if you have the focus in a TextBox, and press the 'R' key, IView::KeyDown() will return false (TextBox only handles specific keys it needs like backspace or delete, arrow keys...), so if there is a shortcut associated with the 'R' key it will be executed. Then the 'R' key is translated into the corresponding char and sent to the IView::Char(), and TextInput event is generated, so TextBox writes the 'r' letter too.The current state is that we are handling half of the shortcuts only after IView::KeyDown() returned explicit "false", outside Noesis. Which is working perfectly fine for actions which are entirely independent from the UI, with no unwanted side effects.
Totally agree, that is why we are trying to offer the best approach with the tools we have available in Noesis.And maintaining keybindings in two different systems is not desirable either.
As I said before that is not the case, because KeyDown will always occur before TextInput. Which leads me to another possible solution, that could be more in line with what you are explaining. Implement a ShortcutTrigger that listens to TextInput event and fires the action on the KeyUp event, if no TextBox handled the TextInput event, something like this:Trying to move the shortcut definitions to Noesis scope has led up to using KeyTrigger (as the only available tool), but with the result of evaluation order of shortcuts and text input getting flipped
Code: Select all
void ShortcutTrigger::OnAttached()
{
ParentClass::OnAttached();
Noesis::UIElement* source = GetAssociatedObject();
if (source != 0)
{
source->TextInput() += MakeDelegate(this, &ShortcutTrigger::OnTextInput);
source->KeyUp() += MakeDelegate(this, &ShortcutTrigger::OnKeyUp);
}
}
void ShortcutTrigger::OnDetaching()
{
Noesis::UIElement* source = GetAssociatedObject();
if (source != 0)
{
source->TextInput() -= MakeDelegate(this, &ShortcutTrigger::OnTextInput);
source->KeyUp() -= MakeDelegate(this, &ShortcutTrigger::OnKeyUp);
}
ParentClass::OnDetaching();
}
bool ShortcutTrigger::CheckModifiers() const
{
Noesis::Keyboard* keyboard = GetAssociatedObject()->GetKeyboard();
return GetModifiers() == keyboard->GetModifiers();
}
void ShortcutTrigger::OnTextInput(Noesis::BaseComponent*, const Noesis::TextCompositionEventArgs&)
{
mTextInputIgnored = true;
}
void ShortcutTrigger::OnKeyUp(Noesis::BaseComponent*, const Noesis::KeyEventArgs& e)
{
if (e.key == GetKey() && CheckModifiers() && mTextInputIgnored)
{
mTextInputIgnored = false;
InvokeActions(0);
}
}