Select to view content in your preferred language

open a widget with keyboard shortcut

775
6
03-23-2011 07:29 AM
RobertAndren
New Contributor II
I am just starting out with flex using the ArcGIS Viewer for Flex and was wondering how to open a widget (ex. Bookmarks) with a keyboard event.
Tags (2)
0 Kudos
6 Replies
JohnGarvey
Deactivated User
Good idea.

I would register a keyboard event and then have a listener that looksa for a particular character code and dispatches an appEvent to openWidget when the character matches the shortcut.

You could add the keyboard event listener to the widget manager.
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Actually I think you would have to have the KeyboardEvent listener added to the stage in the main application (i.e. index.mxml).


Something like this added to the index.mxml on application complete event:

stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown);


and then use a dispatch event like this to launch the widget:

ViewerContainer.dispatchEvent(new AppEvent(AppEvent.WIDGET_RUN, id));


Where id is the number of the widget (each widget as it is loaded is given a sequential number starting at 0).
0 Kudos
RobertAndren
New Contributor II
I added the KeyboardEvent listener to the stage in the main application like Robert suggested but the user has to click inside the map before the keyboard shortcut works.  Would it be possible enable the shortcut without first clicking in the map?
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Robert,

   The Flex Viewer application does not have focus when the app is launched so you have to add some code in the index.mxml to force it to have focus immediately.

Add this script block to the index.mxml:

<fx:Script>
  <![CDATA[   
   protected function init():void
   {
    navigateToURL(new URLRequest("javascript: document.getElementById('index').focus();"), "_self");
   }
  ]]>
 </fx:Script>
0 Kudos
RobertAndren
New Contributor II
That did the trick.  Thanks for all your help.
0 Kudos
MarkHoyland
Occasional Contributor II
Or if you wish to try, here is an xml configurable keyboard shortcut widget.

The widget config file lets you put in the widget label (as defined in the viewers main config xml),
the key and optional boolean values for ctrl, alt and shift keys. The default for  these is false.

eg  key=s and ctrl=true is CTRL+s
key=s and ctrl=false is s

Add to your main config file in the UI Elements part

<!-- UI elements -->
    <widget config="widgets/KeyboardShortcuts/KeyboardShortcutsWidget.xml"
            url="widgets/KeyboardShortcuts/KeyboardShortcutsWidget.swf"/>


Let me know if you think this is useful, and I can upload to the code gallery with compiled version.
0 Kudos