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
https://forums.phpfreaks.com/topic/128805-maintaining-focus-problem/
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?   

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.