There are times when writing mobile apps that you want to have more fine grained control over the software keyboard and Flex 4.5 makes this very easy.

Any object based on InteractiveObject now has a needsSoftKeyboard property, setting this to true will cause the virtual keyboard to come up on the device whenever the instance is given focus.

For example here we have a Button component where we can set the needsSoftKeyboard property to true. As soon as it gets focus when clicked the soft keyboard will trigger. By default the Button component obviously doesn't have this behaviour.

 

[cc lang="mxml"]

[/cc]

 

Apart from setting the needsSoftKeyboard property you can also control it through an ActionScript call. In the example below we trigger the soft keyboard to appear as soon as the view is ready.

[cc lang="mxml"]

[/cc]

 

When the virtual keyboard triggers there are a few events that fire. The first one is "softKeyboardActivating" that indicates the keyboard is about to open and "softKeyboardActivate" which is dispatched right after the keyboard is made available. Likewise there is a "softKeyboardDeactivate" event that gets fired after the virtual keyboard is dismissed.

The SoftKeyboardEvent that gets triggered has two interesting properties, one is triggerType and can have either a value of "contentTriggered" (when the keyboard was triggered through ActionScript) or "userTriggered" when it was triggered through user interaction.

Another property we can get through the event is relatedObject, which is a reference to the InteractiveObject instance that is responsible for triggering the soft keyboard.

[cc lang="mxml"]

private function onActivate(evt:SoftKeyboardEvent):void { trace("Soft keyboard is now active"); trace("Trigger: "+evt.triggerType); trace("Related object: "+evt.relatedObject); }

private function onDeactivate(evt:SoftKeyboardEvent):void { trace("Soft keyboard has been dismissed"); trace("Trigger: "+evt.triggerType); trace("Related object: "+evt.relatedObject); }

]]

Posted
AuthorPeter