No announcement yet.
  • Filter
  • Time
Clear All
new posts

    Binding keypress events to IButtons

    In an application that needs to have key shortcuts to activate buttons, how does one bind a key combination, like control-shift-A to activate a Button? I don't see the "keys" attribute for Buttons. Also, assuming you can do this, in an application that opens multiple Windows, and those Windows want to listen for keypresses, how does the page know which window has focus? Is there a concept of Window focus so my controller can implement alt-tab window focus switching?

    Hi Pallen
    2 options for giving buttons shortcut key type support:

    1 - Set an accessKey on the button - this will ensure that when the user hits Alt+key (or Shift+Alt+key in Mozilla browsers), focus will go to the button and the user can hit 'Enter' to actually fire the click handler

    2 - Use Page.registerKey() to set up a global keypress handler that explicitly calls when the user enters appropriate key / key-combo.

    Both these approaches are application wide - if you have a button inside a Window instance, and you want a keyboard shortcut to only fire a button's action if the window is at the front, you'll have to write some focus tracking logic as well.

    This is not too difficult. One possible approach would be:

    Create a subclass of the Window class. Set canFocus to true, and add a method to
    - Bring the window to the front of the page's Z Index [call this.bringToFront()]
    - Put keyboard focus into the window [call this.focus()]
    - track this window as having 'application focus' somewhere [for example isc.currentFocusWindow = this.getID()]

    When creating your application Windows, track them in some global array (for example isc.openWindows).

    Register a key-combo to switch between these windows
    (Note: You won't be able to use Alt+Tab - this event will take focus from the browser at the OS level, but an alternative combination such as Ctrl+Alt+Tab should work).
    When the user hits this key combination, call the new window-focus method on the window after the current focus window in your array of open windows.

    When you call Page.registerKey() to set up your button shortcut keys, add a conditional to only fire the action if the current focus window contains the button in question as a descendant (using window.contains())

    Hope this helps

    Isomorphic Software


      Another approach, is this better?

      Can't each window call registerKey() and unregisterKey() when they gain and lose focus?

      That way, my controller can listen for Control-Alt-Tab, cycle through Windows with bringToFront() and focus(), and call a new function on each Window like registerKeyResponses() and unregisterKeyResponses() that in turn calls the above API functions to register the appropriate keyboard shortcuts.

      This way, only the focused Window has any keys registered at the Page level.


        So every time the user hits Ctrl+Alt+Tab, you'd run 'unregisterKeyResponses()' on the window that currently has focus, figure out the next window to put focus in, and 'registerKeyResponses()' on that window [as well as actually focussing and bringing to front]?
        Yes actually that seems pretty clean.

        A couple of things to bear in mind:
        - if you're using 'Page.registerKey()' / 'Page.unregisterKey()' to set and clear the shortcut key events on specific buttons within windows, you probably want to pass in the button as the "target" parameter (to both methods).

        This will make the button available to the handler function as a parameter ("target"), and will make your unregisterKey() call unambiguous as to which button's shortcut key you're clearing.

        - We haven't touched upon the case of a user clicking on a window to give it focus (rather than using keyboard navigation).
        By default windows bring themselves to the front on 'mouseUp' (which may be bubbled up from a child of the window).
        You probably want to override mouseUp on your windows to call the same focussing logic that you're firing on ctrl+Alt+keypress, so in this case
        -- Call unregisterKeyResponses() on the previously focussed window
        -- Call registerKeyResponses() on the clicked window
        -- Bring the clicked window to front, and give it focus.

        Let us know if this isn't clear, or you get stuck

        Isomorphic Software