开发者

Capturing Adobe Air keyboard events in new window

开发者 https://www.devze.com 2023-01-07 14:14 出处:网络
This is sufficiently important to me that I\'m opening the question up to a bounty with a simple goal: Can anyone successfully open a new, full-screen NativeWindow in AJAX Air and, from that window, d

This is sufficiently important to me that I'm opening the question up to a bounty with a simple goal: Can anyone successfully open a new, full-screen NativeWindow in AJAX Air and, from that window, detect key strokes?

Hopefully I'm just overlooking something really, really simple, but if JS is not capable of listening for keyboard events, maybe a flash widget/helper might be able to relay keyboard events to JS. That's t开发者_开发知识库he only thing I can think of, but there may be other ways. I just don't know. Hopefully someone out there knows the right answer!


Update

Many thanks to @mwilcox for the answer. I don't know what the difference is between the method I was using (from the O'Reilly Cookbook) and createRootWindow(), but whatever it is, it did solve my problems. The code I ended up using is this:

var objWindowOptions = new air.NativeWindowInitOptions();
objWindowOptions.transparent = false;
objWindowOptions.systemChrome = air.NativeWindowSystemChrome.STANDARD;
objWindowOptions.type= air.NativeWindowType.NORMAL;

var linkScreenToMainWindow = function() {
    wWindow.removeEventListener(air.Event.COMPLETE,linkScreenToMainWindow);
    objScreen.setWindowReference(wWindow.stage.getChildAt(0).window);
    // At this point your windows are connected and you can fire commands into
    // the window using objScreen as a proxy. For example:
    alert(objScreen.document.body.innerHTML);
    objScreen.myfunction();
};

var fhFilePath = air.File.applicationDirectory.resolvePath('childwindow.html');

wWindow   = air.HTMLLoader.createRootWindow(true, objWindowOptions, true);
wWindow.stage.displayState = window.runtime.flash.display.StageDisplayState.FULL_SCREEN_INTERACTIVE;
wWindow.addEventListener(air.Event.COMPLETE,linkScreenToMainWindow);
wWindow.load(new air.URLRequest(fhFilePath.url));

I've created a new window in Adobe Air (JS) and I need to capture any key-presses (or keydowns if it's easier). I have no problem adding an event listener to the main window, but any child window doesn't seem to recognize any of the common hook techniques.

Part of the problem, I think, is that the first parameter of addEventListener() is the name of the event, and all of the documented event names fail to raise any events. Any idea what how I'm supposed to do this?

Main window

// Keyboard handler and event listener subscription:
var watcher = function() {
    alert("Working");
};
window.addEventListener("keypress",watcher,false); // WORKS!

// Create child window:
var wWindow = new air.NativeWindow(objWindowOptions);
wWindow.activate();

wWindow.stage.displayState = window.runtime.flash.display.StageDisplayState.FULL_SCREEN_INTERACTIVE;
wWindow.stage.scaleMode = "noScale"; 
wWindow.stage.addChild( htmlView ); 
htmlView.load( new air.URLRequest("newpage.html") );

Child window: newpage.html

// Keyboard handler and event listener subscription
var handler = function() {
    alert('success!');
};
var strEventName = KeyboardEvent.KEY_DOWN; // Fails -- is undefined
//var strEventName = KeyboardEvent.KEYDOWN; // Fails -- is undefined
//var strEventName = 'keydown'; // Fails
// var strEventName = 1024; // Fails

window.nativeWindow.stage.addEventListener(strEventName,handler,false); // Fails
nativeApplication.addEventListener(strEventName,handler,false); // Fails
window.addEventListener(strEventName,handler,false); // Fails

I may be mistaken, but I think I've tried every permutation of the above and none of them work.


I think you're missing a step. Try:

var newWin = air.HTMLLoader.createRootWindow(...options);
var container = newWin.window.nativeWindow;

Otherwise, are you sure AIR is loaded in that child window?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号