In a previous post, I talked about how Adobe Air was swallowing up the Javascript keydown event for the ctrl+v key combination. Now I have figured out a way to bypass the key events altogether and tap directly into the copy, cut and past events.

These events get triggered whenever a copy, cut or paste occurs, and is consistently triggered within Adobe Air (and any other browser for that matter).

Now, the element on the page I wanted to attach the event to has an element within it that gets dynamically generated, and no amount of tweaking would get the dynamic content to trigger these events within Adobe Air. In Firefox, it did all work as expected.

So my solution for this slight problem, is add the copy/cut/paste event listeners to the window (which would always trigger – dynamic or not) and then add listeners for the focus and blur events on the element I want the editing events trigger for.

There is one more hack needed though. Adobe Air, for some unexplained reason was firing the copy/cut/paste events twice each time I triggered it, so I put in a flag which checkes if the same event is refiring and ignore it.

So the code below, will give selective copy/cut/paste functionality in Adobe Air with no hassles.

var eventHandled = false;
var elementSelected = false;

pasteHandler = function(e) {
	if (eventHandled == false) {
		if (elementSelected == true) {
			alert('Paste');
		}
	}
	eventHandled = !eventHandled;
}

copyHandler = function(e) {
	if (eventHandled == false) {
		if (elementSelected == true) {
			alert('Copy');
		}
	}
	eventHandled = !eventHandled;
}

cutHandler = function(e) {
	if (eventHandled == false) {
		if (elementSelected == true) {
			alert('Cut');
		}
	}
	eventHandled = !eventHandled;
}

elementFocusHandler = function(e) {
	elementSelected = true;
}

elementBlurHandler = function(e) {
	elementSelected = false;
}


document.getElementById('myelement').addEventListener('focus', elementFocusHandler, true);
document.getElementById('myelement').addEventListener('blur', elementBlurHandler, true);
window.addEventListener('paste', pasteHandler, false);
window.addEventListener('copy', copyHandler, false);
window.addEventListener('cut', cutHandler, false);

Share