Keypress to open a URL?

can I open a URL via the press of a specific key (in this case, down-arrow)?

I’m using this JS in the head of my file to go back and forth within the timeline:

document.onkeydown = checkKey;

function checkKey(e) {

	e = e || window.event;

	if (e.keyCode == '39') {
		hypeDocument.continueTimelineNamed('Main Timeline', hypeDocument.kDirectionForward, false);
	} else if (e.keyCode == '37') {
		hypeDocument.continueTimelineNamed('Main Timeline', hypeDocument.kDirectionReverse, false);
	}

}

and would like to add to that… but I’m not seeing the specific syntax for opening a URL via specific keypress using JS. is that a thing?

@patricking

This works for me (down arrow) - added to your current code.

else if (e.keyCode == '40') {
			window.open("http://www.google.com", target="_self"); // use target="_blank" for new window
		}
2 Likes

ok scrap that … Jim got here first!

lol I assumed it would be something something hypeDocument.openURL(‘soemthing something’). leave it to me to make something hard. thanks you two.

@patricking

hypeDocument.openURL('soemthing something')

In the interest of completeness - You actually aren't that far off. You could use a "Custom Behavior", which in this situation is more work; but I have always found when others on the Forum expand the possibilities, sooner or later that technique shows up as useful in some other scenario.


In the "Scene Inspector" I added a "Custom Behavior" named "newURLLoc"


which I then used in the code
} else if (e.keyCode == '40') {
			hypeDocument.triggerCustomBehaviorNamed('newURLLoc');
	     }
1 Like

which is one step too far :smiley: in my opinion. as you have to create the custom behaviour and still call it with Javascript. Whereas you are just using one line of Javascript to call “window.open” but it does highlight another way to do it. I know you mentioned it could be more work. In fact it could be an extension.

hypeDocument.openURL('URL');

hypeDocument.openURL = function(url){
    window.open(url, '_blank');
}

:smiley:

2 Likes

thanks, both of you. this goes a long way in helping me to understand the overall structures. this is my first Hype piece—adding onto a project I started in '15 with Adobe Edge Animate.

the original EA pieces were assembled as several separate presentations connected via a menu document, and controlled via the arrow keys.

arrow-up opens the index file if the presenter gets lost, arrow-down goes to a menu file with all the individual presentations, and left/right go back and forth inside the presentation. this isn’t terribly different from what I created with EA, but just enough to be a little confusing. got it now. thanks again.

2 Likes

I hope I am not hijacking the thread…

I have been trying to animate between pauses of my Main Timeline using similar but different JS.

I tried the JS in the first post, but may have been inserting it incorrectly in the head, as I got no result.’

After reading multiple other threads, I found that pasting the following:

var key = function (e) {
e = e || window.event;
var k = e.keyCode || e.which;

 //L
if (k == 37) {
hypeDocument.continueTimelineNamed('MainTimeline', hypeDocument.kDirectionBackward);
hypeDocument.pauseTimelineNamed('MainTimeline')
};

//M
if (k == 39) {
hypeDocument.continueTimelineNamed('MainTimeline', hypeDocument.kDirectionForward);
};
};

document.onkeydown = key;

into a new function, and then selecting that function in the Scene Inspector -> On Scene Load gives me a part of the functionality I am looking for:

  1. I can hit the right-arrow, and the timeline moves forward, but ignores pauses built into it and runs to the end;
  2. The left arrow pauses the above progress, but pressing it again moves the timeline forward just a bit at a time.

I’m sure I am missing something obvious, but would love to get the timeline to move back and forth between any two pauses with the arrow keys. Please shine a light on my ignorance!

I got it straightened out:

var key = function (e) {
e = e || window.event;
var k = e.keyCode || e.which;

 //L
if (k == 37) {
hypeDocument.continueTimelineNamed('Main Timeline', hypeDocument.kDirectionReverse);
};

//M
if (k == 39) {
hypeDocument.continueTimelineNamed('Main Timeline', hypeDocument.kDirectionForward);
};
};

document.onkeydown = key;

Thank you very much!