I'm trying to manage events in firetv app, the problem is on event fires Enter key and Click key at the same time, how can I prevent that?
Tried to use inEvent.stopImmediatePropagation(); but it still does not help.
$(document).on('click keydown', '.currentSlide', function (inEvent) {
var keycode;
var logger = $('#error-logger');
if(window.event) {
keycode = inEvent.keyCode;
} else if(e.which) {
keycode = inEvent.which;
} if (inEvent.type === 'click') {
keycode = 1;
}
console.log(`Keycode: ${keycode}`);
});
Clicking the remote button triggers the event twice and I'm trying to get it to only do it once.
Solved my problem by adding 1 more var that I set to true on keydown and on keyup to false again.
I'm not sure if there is better way of doing it but this will do for now.
Write the following as the first line in your function before you do anything
inEvent.stoppropagation()
this will stop any further events from triggering.
Related
I want a way to detect the backspace and the enter key strokes.
The most recommended way I found was the OnKeyListener. but, that isn't working for me. I tried Logging and turns out it just wasn't detecting any keystrokes.
Even it's documentation says it won't get triggered always.
I use Microsoft's swiftkey keyboard.
I also got a suggestion for addTextChengedListener. but, that won't detect backspaces. So, can't use it.
here's the code that isn't working -
editText.setOnKeyListener { view, i, keyEvent -> //not working
Log.i(TAG, "onKeyListener working")
when (i) {
KeyEvent.KEYCODE_ENTER -> {
fileChanged(
position,
editText.text.toString(),
note.isAListItem,
note.listItemIsChecked
)
listAdder(position)
true
}
KeyEvent.KEYCODE_DEL -> {
fileChanged(
position,
editText.text.toString(),
note.isAListItem,
note.listItemIsChecked
)
if (editText.text.isNullOrEmpty() && position != 0) {
listRemover(position)
}
true
}
else -> false
}
}
Also, I think the code is being discarded while compiling because the setOnKeyListener function doesn't get highlighted [yellow highlight] as the addTextChangedListener function. This is after the code was compiled. see below -
I have been searching around to find a generic way to use the back key on android devices to go back to the previous scene you were on. All I seem to find is how to make it so the button does not close the application.
Here is my current code:
NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown, false, 0, true)
function onKeyDown(event:KeyboardEvent):void
{
if( event.keyCode == Keyboard.BACK )
{
event.preventDefault();
event.stopImmediatePropagation();
//handle the button press here.
}
}
Just tested it on my Android device, just change trace() with your function:
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
private function onKeyPressed(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.BACK)
{
event.preventDefault();
//Now you can call your function.
trace("Hello World");
}
}
When the selectAll checkbox is tapped, the onChange() event is triggered twice.
This happens only on Android devices with Gingerbread(2.3). On other devices and desktop browsers the event is triggered only once.
Here is the script which I'm using.
/* Check-Uncheck */
self.elements.listview.on('change', ':checkbox', function (event) {
var unchecked = $(':checkbox:not(:checked)', self.elements.listview);
self.elements.selectAll
.prop('checked', unchecked.length === 0)
.checkboxradio("refresh");
});
/* Check-Uncheck 'Select All' */
self.elements.selectAll.change(function () {
var checkboxes = $(':checkbox', self.elements.listview);
var checked = self.elements.selectAll.is(':checked');
checkboxes
.prop('checked', checked)
.checkboxradio("refresh");
});
Please let me know if anyone has faced this particular issue, or have any suggestions.
Thanks
well, i can't determine the source of the problem...maybe the onclick handler doesn't get overwritten...but an easy workaround would be a lock variable...
var lockCheck = false;
//in onchange code of both functions:
if(lockCheck){
lockCheck = false;
return;
}
lockCheck = true;
[edit] ...well...or Omar's suggestion :)
In my AIR AS3 app I'm trying to override Back Button like this:
NativeApplication.nativeApplication.addEventListener( KeyboardEvent.KEY_DOWN, onKey );
private function onKey(e:KeyboardEvent):void
{
if (e.keyCode == Keyboard.BACK)
{
//stage.addChild(new MainMenuScreen());
//stage.removeChild(this);
//removeEventListener(KeyboardEvent.KEY_DOWN, onKey);
}
}
It seems to me that my code is getting done but the default Android behaviour (App is closed) is executed as well.
Have someone faced this problem?
If you want to stop the app closing, you should intercept the EXITING event:
NativeApplication.nativeApplication.addEventListener(Event.EXITING, exitHandler);
function exitHandler(event:Event):void
{
event.preventDefault();
}
I guess i should add that you can manually close the app with:
NativeApplication.nativeApplication.exit();
Just prevent the default action (closing the application) but make sure you still allow the application to close if there's nothing else to do (by using a readyToClose variable, for example):
private function onKey(e:KeyboardEvent):void
{
if(e.keyCode == Keyboard.BACK)
{
if(!readyToClose)
{
e.preventDefault();
//stage.addChild(new MainMenuScreen());
//stage.removeChild(this);
//removeEventListener(KeyboardEvent.KEY_DOWN, onKey);
}
}
}
Thank you all guys for your help!
The problem was due to my FlashDevelop IDE. I used PackageApp.bat instead of Run.bat, so wrong versions of my .apk were deployed onto device. Hope this post will help others who may face this problem
Here is another way to do it - if you use multiple stages you can put this in frame 1 of your first action panel
NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, CheckKeypress, false, 0, true)
function CheckKeypress(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.BACK:
event.preventDefault();
gotoAndPlay (1);
break;
case Keyboard.MENU:
trace("Menu key is pressed.");
break;
case Keyboard.SEARCH:
trace("Search key is pressed.");
break;
}
}
I'm building a mobile AIR app (Android & IOS) with Adobe Flash Builder 4.6 and I'm having this annoying problem.
Because I want to 'catch' the back-key on Android devices I added the following code to my main class:
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
private function keyDown(k:KeyboardEvent):void {
if(k.keyCode == Keyboard.BACK) {
backClicked(); // function handling the back-action, not important
k.preventDefault();
}
Now somewhere else - nested in some classes - I've got a textfield:
TF = new TextField();
TF.type = TextFieldType.INPUT;
But when I set focus on the textfield the soft keyboard does appear, but I can't type a single character. When I disable the keylistener: no problem.
Seems like the listener is overriding my input field. Is there any workaround on this?
I have also implemented the back button functionality for my mobile apps , but i used to register keydown event only when my particular view is activated and removed the registered when view get deactivated.
in <s:view ....... viewActivate ="enableHardwareKeyListeners(event)" viewDeactivate="destroyHardwareKeyListeners(event)">
// add listener only for android device
if (Check for android device) {
NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, handleHardwareKeysDown, false, 0);
NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_UP, handleHardwareKeysUp, false, 0);
this.setFocus();
}
private function destroyHardwareKeyListeners(event:ViewNavigatorEvent):void
{
if (NativeApplication.nativeApplication.hasEventListener(KeyboardEvent.KEY_DOWN))
NativeApplication.nativeApplication.removeEventListener(KeyboardEvent.KEY_DOWN, handleHardwareKeysDown);
if (NativeApplication.nativeApplication.hasEventListener(KeyboardEvent.KEY_UP))
NativeApplication.nativeApplication.removeEventListener(KeyboardEvent.KEY_UP, handleHardwareKeysUp);
}
private function handleHardwareKeysDown(e:KeyboardEvent):void
{
if (e.keyCode == Keyboard.BACK) {
e.preventDefault();
// your code
} else {
}
}
private function handleHardwareKeysUp(e:KeyboardEvent):void
{
if (e.keyCode == Keyboard.BACK)
e.preventDefault();
}
May this can help you.