React state based on mouse/keyboard press/release - html

i'm creating a custom button with React.
The appearance based on the press/release of the mouse/keyboard.
Sometimes it cannot be done by css using :active pseudo, so i need the javascript solution.
So i created a state that listen the onMouseDown/Up & onKeyDown/Up.
The problem is, if the user click the button then drag the mouse out of the button then release the mouse,
i miss the onMouseUp event thus causing the activeState locked up forever.
Do you have a solution for the equivalent of the :active & :not(:active) pair in javascript way?
note:
watching event window.addEventListener('mouseup',...) is still not satisfying me. When the mouse click hold & move out from the browser's window, i still missing the mouseup event.

Related

How can I monitor focus events in Chrome?

I'm trying to track which element gets focus in a web app. I came across the monitorEvents API, but I'm having difficulty using it for control or focus events. Other events on body are working as expected, but not the control events. Any advice?
I'm not sure how exactly you want to "monitor" control events, but you can set event listener breakpoints on the entire category, or individual events like focus. Whenever a focus listener runs for any node on the page, DevTools pauses on the first line of the listener.
I was trying to monitor focus and blur events on the entire document today.
When using monitorEvents(document.body, 'control') I would only see the event fired when I left the tab entirely and then refocused. I believe what's happening is that once you're focused on the body, it'll never change when you focus on child elements since the listener is at a high level and the events just bubble up to the body with no change in focus.
I tried monitoring events on a few specific child elements and saw that those triggered how I expected. So I decided to add an event listener to each element in the document and that worked well. Here's the one-liner for that.
document.querySelectorAll("*").forEach((el)=> monitorEvents(el, 'control'))

ContextMenu detection

(Apache Flex) Right Click brings up the ContextMenu, which I have changed the contents of. All works fine except the MOUSE pointer was hidden to use a custom pointer. The problem is my custom pointer does not move while the context menu is active, nor does the system pointer show because it is hidden. How do I detect the Activation and Deactivation of the Context Menu? An event listener would toggle Mouse show/hide to cure the problem, but I cannot find one for Context Menu De/Activation.
Aaron's solution to use Mouse.cursor worked. Closing.

Spark Button trapping keydown (spaceBar) event

Update: Apparently this is part of the accessibility scheme of Flex Spark Components
Button control Press the Spacebar to activate the Button control. To cancel activating a button, press the Tab key to move the focus off
the Button control before releasing the Spacebar.
I guess it can be turned off through compiler directives: Accessibility best practices
Question: Is there any reason a Spark Button would trap key events, in particular a "spacebar" key event?
Background: I've inherited and am maintaining a large legacy project done in Flex 4.6. I am seeing a weird behavior with a Spark Button. Essentially, once the button has been clicked on (i.e. given focus) a keyEvent (spaceBar) will trigger the click event handler attached to the button.
Weird, right?
The button is defined in MXML (below) within an MX:Module. The module has key event listeners attached to the stage but the event handlers for those do nothing with the button:
this.stage.addEventListener(KeyboardEvent.KEY_DOWN, echoKeyDownHandler);
this.stage.addEventListener(KeyboardEvent.KEY_UP, echoKeyUpHandler);
If I put a trace statement in the button's event handler to check the event type when this weird behavior occurs, the type is reported as a click. I don't see anything in the docs for the Spark Button about capturing key events like this.
<s:Button id="toggleBtn"
label="Editor"
click="toggleBtn_clickHandler(event)"
x="943" y="8"/>
This is quite normal with Flex. You should also be able to navigate through the interactive elements by using the <tab> key. <space> usually acts like a click. You could try to avoid this by either
a) set the focus to another object after pressing the button
or
b) by checking the keyboardPressed property.
Both is a bit hacky imho. I am not sure if it is possible to turn of keyboard navigation completely in Flex.
[edit 1] I am just thinking if it wouldn't help to simply overwrite the default keyboard handler like this:
override protected function keyDownHandler(evt:KeyboardEvent):void {}
this is inherited by UIComponent. and you would need to create an own component extending s:Button. A little drawback...
[edit 2] I wasn't able to stop thinking about that, even I stopped using Flex 2-3 years ago. So I googled a bit and found this stack overflow post:
How to disable default browser navigation with Space in Flex

Page flip effect on button press in html5

I'm looking at this:
http://www.netmagazine.com/tutorials/create-page-flip-effect-html5-canvas
However, I have one problem with that - I need to be able to click on the pages, even the edges, without triggering the page turn. I want the pages to turn when a button outside of the canvas is pressed. Is this possible using the base they provided, or do I need to go an entirely different direction?
Yes that can be done.
From what i can see, you need a click event that doesnt trigger the page drag. You need to assign a flag for this.
Let Drag = mouse drag/mouse move, down = mouse down, release = mouse release events respectively.
Initialize your flag variable as false. When a drag event is encountered it becomes true. Otherwise it remains false. As long as it is false when the mouse release event occurs it can be treated as a click. Thats the basic principle behind using mousedown and mouseup as a click event.
You will have to use e.srcElement or e.target to give you the element your cursor is currently positioned over inorder to trigger click functions relative to that element.
If you want a more detailed explanation on the page flip technique then check this out. Helped me lot.

Button removed with removeChild() gets re-added in it's over state

I have a SimpleButton on the stage, and in the click event for it, I remove it using removeChild(). When re-adding the button, it gets re-added in it's "over" state, not the "up" state as expected.
I'm pretty sure it's a bug, so I've filed a JIRA: http://bugs.adobe.com/jira/browse/SDK-31445, my question is now on how to fix it, if anybody's had a similar experience.
What I've tried:
using visible = false instead works, but I'd prefer to remove it off the stage altogether as the game I'm working on can have a lot of popups/screens.
Firing the mouse_out event manually - doesn't work, and in any case, the mouse_out event fires as expected (perhaps before resetting to the up state, it makes a check to see if it's added to the stage, finds out it's not, then quits early)
Resetting the stage focus - I thought it was a problem with the stage focus, as you were losing focus when the button was hidden, but resetting it didn't fix it.
I could temporarily swap the over and up state when it's hidden, but that seems like an ugly fix.
I've been Having this problem as well, removing one button and adding another in response to an Mouse up event on a separate sprite.. IE the mouse isn't over either of the buttons at the time, But when the first button (the one with the problem) was previously removed the mouse was indeed over it so it hadn't received a mouseOut event and retained itself in the over state. Using visibility to work round this which is ok for my needs BUT the alternative answer would be to replace with a new instance of the button before adding it to the Display List.