views:

96

answers:

1

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 the 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.

+1  A: 

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?

mwilcox
I think that's got me closer. I actually was going by what the O'Reilly Cookbook recommends, so I never tried the createRootWindow(). I'm not sure what the difference is between `new air.HTMLLoader.createRootWindow()` and `new air.NativeWindow()`. I see that `HTMLLoader` has an `addEventListener()` method, but I can't get it to fire (trying either `complete` or `html_render`. I need to know (from the first window) when the new window has been created. Do you know how to do that?
Andrew
IIRC, I had to use a setTimeout and check to see if it was available. But I would first check to see if it is built synchronously, like in JS.
mwilcox