Jump to content

maintaining focus problem


shutat

Recommended Posts

I'm working on a game where I have a script that polls the keyboard in order to simulate movement of a central character.  If the character is at a certain coordinate, I change the src property of an iframe to load a midi file.  That's where my problem arises, I lose focus on the character and cannot figure out programmatically how to get it back.  Is it possible?

 

Here's a basic idea of what I'm up to.

 

main.html

<head>
<script language="javascript">
var x, y;

function pollKeyB(e) {
   ...

   if(x == m && y == n) { 
      document.getElementById("midi").src = "sndwrap.php?snd=" + snd_file;
} 
...
</head>
<body onload="init()" onkeyup="pollKeyB(event)">
<iframe id="midi" src="sndwrap.php"></iframe>
<table id="main" ...>
game ui
</table>
</body>
...

 

I've tried everything from setting the iframe's onfocus event to main's onblur event, but none of the combinations I've tried works.  :( 

 

in sndwrap.php I get the sound file via $_GET and then set either an embed or MS's function (forget it's name) to play the midi file.  I even tried setting sndwrap's body onfocus event to blur, but that didn't work either.

 

It seemingly *works* but in order to move the character again, I have to click  inside of the table "main."  Does anyone have any ideas how I keep focus on the keyboard polling, yet still be able to dynamically change sound via the iframe src?

 

I'm not that familiar with JS other than a few docs I've read.  As a side question, what is purpose of returning true or false in some functions?

Link to comment
Share on other sites

You can use document.getElementById("main").focus() to set the focus back onto it.

 

Thanks for the reply - I sure appreciate it. 

 

I tried that in the init function and as a stand alone function for main's onblur event, but it didn't work - I either got .focus() isn't a function in the js console or it didn't do anything when I used .focus

 

Where should it go?   

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.