Rather than assign to every buttons with MouseEvent, I assign to AIR application with:
private function init():void {
this.addEventListener(MouseEvent.MOUSE_DOWN,mpressKey);
}
However, I only want the mouse_down to execute if it detect a "button" property instead of Demo0.WindowedApplicationSkin2.Group3.contentGroup.g4 (g4 is an id).
Don't rely on event.target to check if a button was clicked or not. The target property is set to the innermost item that was clicked on. When you click on a button, you're not always clicking on The Button; you might be clicking on the text field that displays the label, or the background image if any, or some other child skinning part etc - the target will be set to this inner item.
If you want to have a single click handler for all buttons and take appropriate action based on the button clicked, you can assign same function as handlers for each button and check the event.currentTarget property; when an event handler is invoked, currentTarget is set to the object with which that handler was registered.
btn1.addEventListener(MouseEvent.CLICK, clickHandler);
btn2.addEventListener(MouseEvent.CLICK, clickHandler);
btn3.addEventListener(MouseEvent.CLICK, clickHandler);
public function clickHandler(e:MouseEvent):void
{
if(e.currentTarget == btn1){
/* Handle btn1 here */
}
else if(e.currentTarget == btn2){
/* Handle btn1 here */
}
else if(e.currentTarget == btn3){
/* Handle btn1 here */
}
}
When you add a single mouse handler using airApp.addEventListener, the currentTarget will always be your airApp and thus you can't use it to act as a single function to handle them all.
Are you asking how to test to see if the target is a Button, or a particular button?
If it's a Button
if ( e.target is Button ) { ... }
or if it's a particular button
if ( e.target == myButton ) { ... }
Related
I'm using code based on this post.
It uses a focus_out event to detect if there is a change that needs to be committed. However I notice that a FOCUS_OUT event is only called if you click away from the textfield but inside the component. Is there any way I can listen for clicks outside the component from within the component?
addEventListener(FocusEvent.FOCUS_OUT, onFocusOut);
protected function onFocusOut(event:FocusEvent):void
{
_updatedText = text;
if(_updatedText != _originalText){
dispatchEvent(new Event(Event.CHANGE));
}
setEditable(false);
}
In the component itself, you can do this:
systemManager.addEventListener( FocusEvent.KEY_FOCUS_CHANGE, focusChangeHandler );
systemManager.addEventListener( FocusEvent.MOUSE_FOCUS_CHANGE, focusChangeHandler );
Just be sure you clean up and remove the event listener before your component is removed from the stage (assuming it is added dynamically). That will prevent you from stacking up a bunch of event listeners.
Alternatively, if you just want to find out whenever someone clicks outside of a specific component, you can do something like this:
systemManager.addEventListener( MouseEvent.MOUSE_DOWN, system_mouseDownHandler );
private function system_mouseDownHandler( event:MouseEvent ):void {
if( !event.target != this && !this.contains(event.target as DisplayObject) ){
// Do Something Here
}
}
Again, make sure you cleanup any event listeners if this component is added/removed dynamically.
Hope this helps!
EDIT:
If you want to cleanup the eventListeners, do something like this (called when the remove event is triggered in your component):
<mx:Component remove="myRemoveHandler();" />
private function myRemoveHandler():void {
if( systemManager.hasEventListener( MouseEvent.MOUSE_DOWN ) systemManager.removeEventListener( MouseEvent.MOUSE_DOWN, system_mouseDownHandler );
}
Obviously substitute the event listeners that you ended up using (Focus or Mouse).
In LabelEditor class dispatches a Event.CHANGE event on focus out you can just listen for that event
I'm trying to use a flexlib schedule viewer in my application.
I want to have it so that when I click on a scheduled event, it calls a function in my main app (that will allow me to edit the event). But there doesn't seem to be any specific function for anything like this built into the class ie no event dispatched when I click on an event.
I can use the 'click' function to detect that the item has been clicked on.. and have tried something like this:
private function exerciseClickHandler(event:MouseEvent):void{
if (exerciseSeries.selectedItem != null){
//code
}
}
<code:ScheduleViewer id="exerciseSeries" click="exerciseClickHandler(event)" />
This method isn't very reliable because if it only works the first time.. once an item is selected, it stays selected so all following clicks on the item fulfills the condition.
Is there any way to determine whether an event was being clicked on?
Or do I have to extend the component and add some sort of clickEvent when an event is clicked on.
Since exerciseClickHandler is firing up when you click on the component, wouldn't this work?
Instead of
private function exerciseClickHandler(event:MouseEvent):void{
if (exerciseSeries.selectedItem != null){
//code
}
}
write
private function exerciseClickHandler(event:MouseEvent):void{
switch (exerciseSeries.selectedItem)
{
//code
case xy:
break;
}
}
or
private function exerciseClickHandler(event:MouseEvent):void{
//do something with exerciseSeries.selectedItem
}
What I mean is that you wrote that everything stops after the first element is clicked. And according to the code you provided it has to stop, beacuse after the first click exerciseSeries.selectedItem won't be null anymore, since it's selected. So remove the conditional you wrote and use the instance.
I'd suggest you set up a ChangeWatcher to keep an eye on the selectedItem (or selectedItems if you are going to allow multiple selection at some point). Example:
protected exerciseSeriesCreationCompleteHandler(event:FlexEvent):void{
ChangeWatcher.watch(this,['exerciseSeries','selectedItem'], handleChange_SelectedItem);
}
protected function handleChange_SelectedItem(event:PropertyChangeEvent):void{
// Either
dispatchedEvent(//some custom event);
// Or
someDirectMethodCall();
}
An alternative would be to search for an instance of the the event class in the view hierarchy under the mouse coordinates whenever a user clicks.
//Attach this click handler to the component
private function handleClick(event : MouseEvent) : void {
var obj : *EventClass*= null;
var applicationStage : Stage = FlexGlobals.topLevelApplication.stage as Stage;
var mousePoint : Point = new Point(applicationStage.mouseX, applicationStage.mouseY);
var objects : Array = applicationStage.getObjectsUnderPoint(mousePoint);
for (var i : int = objects.length - 1; i >= 0; i--) {
if (objects[i] is *EventClass*) {
obj = objects[i] as *EventClass*;
break;
}
}
if(obj is *EventClass*){
//Dispatch some custom event with obj being the item that was clicked on.
}
}
Where EventClass is the class of the objects that represent events
I have had similar problems and sometimes you can get by with wrapping the object with a Box and putting the click event on the Box. If you have not already tried that, it's a cheap, easy fix (if it works for you).
<mx:Box click="exerciseClickHandler(event)">
<code:ScheduleViewer id="exerciseSeries" />
</mx:Box>
I use a ASP.NET Menu control with Orientation=Horizontal. It is kind of irritating that the popout menus appear on mouseover, which causes it to show by accident if you move the mouse over the menu when you want to click on something right below the menu. Then the menu popout hides the element you actually wanted to click on!
Is it possible to change the functionality so that the popout requires a mouse click instead of mouseover?
Well, I found a solution myself (kind of a hack...).
This solution requires use of AJAX to capture the menu item onclick postback event, so it can be picked up client side in javascript before doing the actual postback when you click the menu item.
First, I override these functions that is defined by the Menu control
to ignore the menu popout in the mouseover event:
var activeMenuItem = null;
function Menu_HoverStatic(item) {
// Register the active item to be able to access it from AJAX
// initialize postback event
activeMenuItem = item
// Apply the style formatting on mouseover (colors etc).
// This was also called in the original Menu_HoverStatic function.
Menu_HoverRoot(item);
}
function Menu_Unhover(item) {
activeMenuItem = null; // This is the only difference to the original
var node = (item.tagName.toLowerCase() == "td") ?
item:
item.cells[0];
var nodeTable = WebForm_GetElementByTagName(node, "table");
if (nodeTable.hoverClass) {
WebForm_RemoveClassName(nodeTable, nodeTable.hoverClass);
}
node = nodeTable.rows[0].cells[0].childNodes[0];
if (node.hoverHyperLinkClass) {
WebForm_RemoveClassName(node, node.hoverHyperLinkClass);
}
Menu_Collapse(node);
}
// Then I added a renamed copy of the original `Menu_HoverStatic` function:
function Menu_ClickStatic() {
// Pick up the active menu item that is set in the
// overridden Menu_HoverStatic function.
// In the original, the item was input parameter.
var item = activeMenuItem;
// The rest is identical to the original Menu_HoverStatic.
var node = Menu_HoverRoot(item);
var data = Menu_GetData(item);
if (!data) return;
__disappearAfter = data.disappearAfter;
Menu_Expand(node, data.horizontalOffset, data.verticalOffset);
}
Then I snap up the onclick postback event in AJAX that is triggered by the menu. This must be done to cancel the onclick postback and display the menu popout instead.
// Get the Page Request Manager that provides all the .NET
var prm = Sys.WebForms.PageRequestManager.getInstance();
// Register postback event for asyncronous AJAX postbacks
if (prm) prm.add_initializeRequest(InitializePostback);
function InitializePostback(sender, args) {
var element = args.get_postBackElement();
//Check if the postback element is the menu
if (element.id == 'myMenu') {
// Name of the menu element that triggered is the postback argument
var postbackArguments = document.getElementById('__EVENTARGUMENT');
if (postbackArguments)
// Check on the menu item name to pick up only the menu items that shall
// trigger the popout (not the items that does an actual command).
if (postbackArguments.value == 'MenuTopItem1'
|| postbackArguments.value == 'MenuTopItem2'
|| postbackArguments.value == 'MenuTopItem3') {
// Abort and cancel the postback
prm.abortPostBack();
args.set_cancel(true);
Menu_ClickStatic(); // Call my own copy of the original function
return;
}
}
}
Note:
I found out the details about these functions by using the script viewer in Firebug.
The soluton provided above doesn't work in everone's case. One can also try this out, it worked in my solution-
var jq = jQuery.noConflict();
jq(document).ready(function () {
jq(document).on('click', '#ctl_id_Here', function () {
Menu_HoverStatic(this);
Menu_HoverRoot(this);
});
jq(document).on('click', '#ctl_id_Here', function () {
Menu_HoverStatic(this);
Menu_HoverRoot(this);
});
});
3 Steps:
Stop the current hovering effects:
On page load (or on ready), write following line: $('#Menu1').find('ul .level2').css('display','none');
Once you do that, it'll stop the hovering effect of that menu. But once you do that, then you would only be able to open the submenu by making it display block, so for that I wrote following lines, onclick of an image inside the menu: $('#Menu1').find('ul .level2').css('display','block');
Open the menu on click of an element: I don't think need to explain it. Just make menu display block on click of the identified element.
Close the opened menu: 2 ways to do it: First; Use property Disapperafter as below:
Second: Write below code to close it onclick of anywhere else on the screen:
$('body').click(function(evnt) {
if($(evnt.target).parents('table#menu').length == 0)
{
$('#MenuInvitePatient').find('ul .level2').css('display','none');
return;
}
else
{
return;
}
});
I've a parent (Canvas) with many children (LinkButtons)
The linkButtons trigger an event to communicate between them:
dispatchEvent(new SameBookmarkEvent("SameBookmarkEvent", bookmark.name));
and all linkButtons have a listener
this.addEventListener("SameBookmarkEvent", highlightMe);
...
private function highlightMe(e:SameBookmarkEvent):void {
//do something
}
Now, the issue is that the event is only listened by the dispatcher child. In other words, only the child triggering the event, is receiving it. I was wondering what's wrong with it, and if I should add a listener to the parent (Canvas)...
I basically need the children (LinkButton) communicate between them
Yes, when you add the button's listener to a function within that object, of course only that object will receive the event. When you call addEventListener with the highlightMe function, you pass a reference to the highlightMe function within the current scope. That means that the private function within that class is referenced. But that function is different for each new instance of the class.
In OOP each object works for itself and doesn't know anything about the parents, so one link button should not be able to know that there are other link buttons beside itself and what those buttons do. Instead the parent knows that there are X different link buttons, each working alone, but managed by the parent to work together. In that sense, when working with events, one should always leave the event handling in the parent object – except your custom class has default handlers that keep a standard procedure working (like changing appearance on mouse over etc), or when you have custom events that encapsulate other events.
As such the correct way to deal with it, is to have a single event handler in the parent class (where you instantiate the buttons) which then also is able to identify which button the event is related to.
Example
public class SomeParent extends Sprite
{
public function SomeParent ()
{
var btn:Button;
for ( var i:uint = 0; i < 100; i++ )
{
btn = new Button();
btn.label = 'Button ' + i;
btn.addEventListener( MouseEvent.CLICK, clickHandler );
this.addChild( btn );
}
}
private function clickHandler ( event:MouseEvent ):void
{
var btn:Button = event.eventTarget as Button;
trace( 'Button with id ' + this.getChildIndex( btn ) + ' and label "' + btn.label + '" was pressed.' );
}
}
I am populating a textfield programmatically and scrolling to the maxScrollH as the new text is added so the user can see the progression of text. This works fine, until I click the TextField, which sets scrollH back to 0 and places the caret in the text at the equivalent position.
textField.setSelection( text.length, text.length ); //sets the caretIndex/selection to the end
textField.scrollH = textField.maxScrollH; //scrolls to max
this is the code that I am using to scroll when the textField text property is updated. I've tried adding a listener to the click event on the textField, which works in a way, but causes a visible jump.
override protected function createChildren() : void
{
super.createChildren();
textField.addEventListener(MouseEvent.CLICK, handleTextFieldClick, false, 0, true);
}
protected function handleTextFieldClick(event:MouseEvent):void
{
textField.scrollH = currentTextFieldScrollPosition; //stored scrollH value
trace(textField.scrollH);
}
My guess is that there is a scroll position being calculated or stored someplace that I can't find.
The flex TextInput sets the selection of the textField:
/**
* #private
* Gets called by internal field so we draw a focus rect around us.
*/
override protected function focusInHandler(event:FocusEvent):void
{
if (event.target == this)
systemManager.stage.focus = TextField(textField);
var fm:IFocusManager = focusManager;
if (editable && fm)
{
fm.showFocusIndicator = true;
if (textField.selectable &&
_selectionBeginIndex == _selectionEndIndex)
{
textField.setSelection(0, textField.length);
}
}
[...]
overriding this in my component fixes this problem:
override protected function focusInHandler(event:FocusEvent):void
{
super.focusInHandler(event);
textField.scrollH = currentTextFieldScrollPosition;
}
Turns out you need to make an onclick handler for the window with the UIScrollbar, and then add a focus out event listener for it that passes the scrollbar location at the time of the onclick to the focus.
ActionScript 3: maintaining textarea UIscrollbar position on loss of focus in flash embed