Detecting KeyBoard events inside NumericStepper - actionscript-3

I have a movie clip with 2 buttons (Ok and Cancel) and one Numeric Stepper.
If the user press enter key on the keyboard I want that "Ok" button function runs and if "BackSpace" is pressed "Cancel" function.
I have this code that detect when I press almost all the keys but not when I press "ENTER" or "BACKSPACE".
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyPressed);
function onKeyPressed(event:KeyboardEvent):void
{
if (event.keyCode==Keyboard.ENTER) {
okBtnFunction();
}
if (event.keyCode==Keyboard.DELETE) {
cancelBtnFunction();
}
}
I also tried with event.charCode.

Enter and Backspace keys aren't enable because of the flash shortcut, you can see it in this link:
Flash AS3: ENTER does not get detected, but CTRL+ENTER works fine
If you try to get keyboard event inside NumericStepper you have to use:
Object(this).YOU_NUMERIC_STEPPER.textField.addEventListener(KeyboardEvent.KEY_DOWN, keyboard_handler);
Remember to use textField after numeric stepper name.

Related

Capturing the Paste option from the context menu in AS3 Air Application

Here is what I am trying to do:
User right clicks on a textfield, the system menu that shows the options of "cut,copy,paste,delete,Select All" appears (with only "Paste" and "Select all" active)
User clicks on "paste"
The pasted text is added to the textfield.
My issue is being able to run code right after the user selects the "paste" option from the contextmenu. I tried listening to the textfield change, eventchange, to no avail. This is the code I am using. When the above happens, the following code does not fire. "d" is the textfield object
d.addEventListener(Event.CHANGE, paste);
private function paste(e: Event): void {
trace("paste event fired");
if(Clipboard.generalClipboard.hasFormat(ClipboardFormats.TEXT_FORMAT)) {
trace("pasted data is ", String(Clipboard.generalClipboard.getData(ClipboardFormats.TEXT_FORMAT)));
}
}
UPDATE:
I tried the suggestions below, but still no luck. Here is the code I have. "d" is already added to stage
private function start():void {
d.addEventListener(TextEvent.PASTE, paste);
}
private function paste(event:TextEvent):void{
trace("something got pasted");
}
What's bugging me is that event is not firing for whatever reason
A TextField is an InteractiveObject, which has a paste event. You can listen for it with the flash.events.Event.PASTE constant.
EDIT: Apologies, the paste event documentation says it doesn't work with TextField. It recommends using the Flash Text Engine, though that is difficult and I don't know how to do text input with it.
You could try:
The textInput event.
The paste, change, and textInput events with a TextInput component instead of a TextField.
The paste, change, and textInput events with a TextArea component instead of a TextField.

Triggering button event (or atleast appear to) via keyboard press AS3

Attempting to make a piano in AS3 and so far I have 2 methods of triggering a note, 1 via keyboard and 1 via mouse-click. When I click the key, it changes colour to indicate that the key has been pressed. Unfortunately , I can't find a way for the keyboard event to trigger the button press, is it possible and how would I do it?
It doesn't need to be an actual button press, it can instead just appear to be.
EDIT
The button is a button object, using the GotoAndStop causes the following error code:
"project.as, Line 13, Column 7 1061: Call to a possibly undefined method gotoAndStop through a reference with static type flash.display:SimpleButton."
Thanks in advance.
The easiest way is to extract the piano key press logic into its own function, and then call that function from two events.
function pressedMyPianoKey(){
// Do something like pianoKey.gotoAndStop(2) to show a new graphic
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
function keyDown(event:KeyboardEvent):void
{
pressedMyPianoKey();
}
pianoKey.addEventListener(MouseEvent.CLICK, click);
function click(e:MouseEvent){
pressedMyPianoKey();
}

Navigation from frame 1 to frame 2 renders event listeners non-functional in flash/actionscript3

I am new to actionscript and flash, and I have been unable to find a good explanation for why I cannot get this pretty basic code to work. I am using flash cc with actionscript 3. I have two frames on my timeline, one labeled "startFrame" and another labeled "newFrame". In "startFrame", I have a button with the instance name "btnStart". There is an event listener that listens for a mouse click on btnStart and then calls a function that goes to the next frame. Here is the code for "startFrame":
stop();
//When "btnStart" button is clicked, call the beginSession function.
btnStart.addEventListener(MouseEvent.CLICK, beginSession);
function beginSession(event:MouseEvent):void
{
//Remove event listener??
//btnStart.removeEventListener(MouseEvent.CLICK, beginSession);
//Go to newFrame.
gotoAndStop("newFrame");
}
I thought initially I should remove the event listener in the function after it is called, but it doesn't seem to work whether I remove the event listener or not. Here I have it commented out. In the "newFrame" frame, I have an event listener that listens for a key press and then calls a function that returns the key and character codes for that given key. That code is here:
stop();
//show that newFrame has been reached.
trace("newFrame has been reached");
//Add event listener for a key press to the stage.
stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);
//function that returns the key pressed and the character code.
function reportKeyDown(event:KeyboardEvent):void
{
trace("Key Pressed: " + String.fromCharCode(event.charCode) + " (character code: " + event.keyCode + ")");
}
When I test the movie, the button press works and "newFrame" is reached (the "newFrame has been reached" trace is found in the output). But it does not seem that the event listener for the key press is working. However, if I create a project that only has "newFrame" by itself (no other frames in the timeline), it works just fine when I test the movie. Only after I add in the startFrame and the button navigation do the event listeners seem to no longer hear the key press event in the second frame. Also, I am not getting any compile errors. What am I missing here?
Disable keyboard shortcuts under view while testing the application.

flash - getting keyboard focus without clicking

In an Actionscript 3 program I used this code to add an event listener to a TextField:
var tf:TextField = new TextField();
tf.addEventListener(KeyboardEvent.KEY_DOWN, handleText);
handleText() only gets called if I click on the TextField. Is there a way to receive keyboard events when the mouse is just hovering over the TextField (no need to click)?
A TextField has its own keyboard event listener that it uses to alter text, if its mode is input. And to capture keyboard events while your target does not have focus, add the listener to stage, and check if your mouse is over the text field in question. For this, you can set a boolean flag in MOUSE_OVER and drop it in MOUSE_OUT listener attached to the textfield. Then, if the flag is on, you can either parse the keyboard event yourself or transfer it to the TextField via dispatchEvent() call with existing KeyboardEvent object.

How to detect if the delete key was pressed in Actionscript 3?

How do I determine if the delete key was pressed using actionscript?
addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
...
function onKeyUp(event:KeyboardEvent):void
{
trace(event.keyCode);
}
The above code yields no value when delete, backspace, enter, and other command keys are pressed. However, arrow keys do yield values.
this.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
....
function onKeyPressed(event:KeyboardEvent):void
{
if (event.keyCode==Keyboard.DELETE) {
.....
}
}
it's workin nice...
But if you test movie from Flash, it will not work, so export to swf and test....
Just guessing you are using the TEXT_INPUT event, this doesn't work for delete and backspace. To catch those ones you can add an eventListener on the stage and listen to a KeyboardEvent.
Code will work fine if the display object that you attached the listener is in focus. For global listening, as Theo said, you have to attach the listener to the stage. Accessing stage from an object that's not yet added to the display list will result in null error. Do it in the ADDED_TO_STAGE event handler to be safe.
Old thread, but if anyone gets this far: in the Flash Player inside the IDE, these keys are associated with shortcuts. When you test your movie, choose Control>disable keyboard shortcuts in the player and you'll get the events back.