Roest
Topic Author
Posts: 28
Joined: 15 Jan 2020, 15:30

Disable keyboard shortscuts when in gui elements

15 Jun 2020, 14:58

I'm using keyboard shortcuts in the way you suggested here. That works well as expected but now I encountered a new problem. These shortcuts also fire when editing text in a TextBox for example. Is there a way to avoid that?
 
User avatar
sfernandez
Site Admin
Posts: 1915
Joined: 22 Dec 2011, 19:20

Re: Disable keyboard shortscuts when in gui elements

15 Jun 2020, 17:06

Do you want to disable the shortcuts when it is editing an specific TextBox?
Would it be possible to filter out the KeyTrigger so it is only invoked when that TextBox is not focused?
<ei:KeyTrigger Key="A" FiredOn="KeyUp">
  <i:Interaction.Behaviors>
    <ei:ConditionBehavior>
      <ei:ConditionalExpression>
        <ei:ComparisonCondition LeftOperand="{Binding IsKeyboardFocused, ElementName=textbox}" RightOperand="False"/>
      </ei:ConditionalExpression>
    </ei:ConditionBehavior>
  </i:Interaction.Behaviors>
  <ei:ChangePropertyAction PropertyName="Text" Value="Hello" TargetName="txt"/>
</ei:KeyTrigger>
Using IsKeyboardFocusWithin property you can apply the same filter to a group of controls when focus is inside one of them.
 
Roest
Topic Author
Posts: 28
Joined: 15 Jan 2020, 15:30

Re: Disable keyboard shortscuts when in gui elements

15 Jun 2020, 17:26

Not talking about a specific textbox here but all. Keyboard shortcuts are global and contain things like rotate map or zoom, then there are the number keys which are shortcuts to certain commands. None of them should trigger when you edit the name of a creature or put in a number for a trade value.
 
User avatar
sfernandez
Site Admin
Posts: 1915
Joined: 22 Dec 2011, 19:20

Re: Disable keyboard shortscuts when in gui elements

15 Jun 2020, 19:05

From your answer I assume your shortcuts are not using modifier keys like Ctrl or Alt, or functional keys as F1-F12, and that is why you are having trouble with the TextBox.

We are following WPF implementation for KeyTrigger, and the behavior as you have seen may look broken when not using modifier keys in shortcuts with a TextBox focused. We are really considering deviating from WPF here and change KeyTrigger to take into account the focused control so we can skip invoking the associated actions. You can try it yourself by modifying the KeyTrigger source code (provided as part of our application framework) to something like this:
#include <NsGui/TextBox.h>
#include <NsGui/PasswordBox.h>

////////////////////////////////////////////////////////////////////////////////////////////////////
void KeyTrigger::OnKeyPress(BaseComponent*, const Noesis::KeyEventArgs& e)
{
    if (GetKey() == e.key && CheckModifiers() &&
        Noesis::DynamicCast<Noesis::TextBox*>(e.source) == 0 &&
        Noesis::DynamicCast<Noesis::PasswordBox*>(e.source) == 0)
    {
        InvokeActions(0);
    }
}
 
Roest
Topic Author
Posts: 28
Joined: 15 Jan 2020, 15:30

Re: Disable keyboard shortscuts when in gui elements

15 Jun 2020, 19:15

You are right we are not using modifier keys or more precise we have triggers without modifier keys because people are used to it. We also looked into it and saw that it's WPF way to have modifiers with key triggers. That might be ok for standard WPF applications but a game gui should allow keyboard shortcuts without modifies imho. I'll try your solution and will report back, thanks.
 
Roest
Topic Author
Posts: 28
Joined: 15 Jan 2020, 15:30

Re: Disable keyboard shortscuts when in gui elements

15 Jun 2020, 19:24

That works, can mark this as solved though I would still prefer that you guys find an internal solution or I'd have to make this change every time you publish an update.
 
Ext3h
Posts: 4
Joined: 07 Mar 2020, 10:14

Re: Disable keyboard shortscuts when in gui elements

16 Jun 2020, 09:48

Modifying the behavior of KeyTrigger to ignore specific inputs got it all backwards. Speaking in terms of semantic, it's not that the KeyTrigger should ignore a specific sequence, it's that the TextBox should (be able to) consume the input it can handle before shortcuts are triggered.

The part where WPF (and Noesis) appears to be behaving weird here, is that the KeyDown / KeyUp events are only handled by the TextBox if not handled by any parent on the way up. Makes sense if you realize that you are at this point only handling the KeyDown event, but it has yet to become text input.

In this case it's not the behavior of a KeyTrigger which is intended though, it's rather the equivalent of access key. In WPF, they entire access key management has a lower priority than the regular (text) input management, so you don't even run into the issue of accidentally consuming key presses which should have been consumed as text input.

Noesis doesn't have an implementation of the whole AccessKey feature suite though. And the right spots to implement that are not exposed to NoesisApp, but rather in the core of NoesisGui. Closest you can get is taking the return value of IView::KeyDown(), and in case Noesis states "not handled", begin hacking around. Which is quite nasty, as access keys are usually also context sensitive, with regard to whatever menu is currently open in the UI.
 
User avatar
sfernandez
Site Admin
Posts: 1915
Joined: 22 Dec 2011, 19:20

Re: Disable keyboard shortscuts when in gui elements

16 Jun 2020, 10:11

I think @Roest can't use access keys in this context, because he doesn't want to use the Alt modifier key, just plain keys like 'R' to enable 'Rotate' mode.

And setting KeyDown as handled is not an option either, because then you wont't be able to write in the TextBox. If KeyDown is handled then TextInput event won't be triggered and text won't be added to the TextBox.
 
Roest
Topic Author
Posts: 28
Joined: 15 Jan 2020, 15:30

Re: Disable keyboard shortscuts when in gui elements

16 Jun 2020, 10:51

I think @Roest can't use access keys in this context, because he doesn't want to use the Alt modifier key, just plain keys like 'R' to enable 'Rotate' mode.
I think the question is for games in general. You want to have keyboard shortcuts without modifier keys. That's quite an accepted and expected thing now. So I'm asking what is the recommended design to handle these shortcuts? Should I do it myself at window level where the keys are captured or should just send key events into Noesis and work with triggers there but then I need Noesis to not trigger triggers when the key is consumed otherwise.
 
Ext3h
Posts: 4
Joined: 07 Mar 2020, 10:14

Re: Disable keyboard shortscuts when in gui elements

16 Jun 2020, 12:33

I'm actually working on the same project. Right, Alt key should not be required per default. It is a sensible option though, when you need to escape from a context where shortcuts are masked, without explicitly yielding keyboard focus first. If you compare to WPF, the alt modifier is also only required to open menus, but once menus are open, no further modifier is required. The need for Alt key outside of menus in WPF is just a design choice to protect against miss-input in applications which are primarily focused on text input.

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. But for actions which are routed via view model (or directly control UI, like opening a menu, triggering buttons including their animations etc.), or are in any form context dependent, that detour isn't viable. And maintaining keybindings in two different systems is not desirable either.

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. Also, using a KeyTrigger is not behaving quite the same as an access key in terms of actually activating an UI element with all the UI related side effects such as focus etc., at least not without having to hard wire the logic.

Your suggested workaround (plus also marking the event as handled if matched, don't want to accidentally trigger too much in case multiple components overload same shortcut in local scope) actually works for getting the shortcuts themselves functional, but not quite for integrating it fully with the UI.

Regarding integrating it with the UI, does Noesis fully support emulating the ClickEvent on BaseButton? If so, that may suffice to emulate full access key support already.

Who is online

Users browsing this forum: No registered users and 1 guest