Current location: Hot Scripts Forums » General Web Coding » JavaScript » Hotkey On Keypress

Hotkey On Keypress

Reply
  #1 (permalink)  
Old 06-21-09, 09:42 PM
smithygotlost smithygotlost is offline
Coding Addict
 
Join Date: Jul 2006
Location: United Kingdom
Posts: 335
Thanks: 2
Thanked 0 Times in 0 Posts
Hotkey On Keypress

Heya Guys

Ive got a scrip that on a given key press uses location.href ='URL' this is great for the way we used to do things, now we made everything java so rather than going to a url i need it to run a javascript function, However after hours of trying i have no idea

i need it to do this

Code:
if (x==key1) location.href='<?echo "charmove($map_link_up,$hex_up)"; ?>';
the full script is below

Code:
<script language="javascript">
var key1="119";
var x='';
function handler(e)
{
  if (document.all) {
  var evnt = window.event;
  x=evnt.keyCode;
}
else
x=e.charCode;
if (x==key1) location.href='<?echo "charmove($map_link_up,$hex_up)"; ?>';
}
if (!document.all){
window.captureEvents(Event.KEYPRESS);
window.onkeypress=handler;
}
else
{
document.onkeypress = handler;
}
 </script>
Any help will be greatly welcomed

Thanks
Mike
__________________
Make People Friendly Say " Thanks "
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 06-22-09, 12:02 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 2,632
Thanks: 0
Thanked 15 Times in 15 Posts
Quote:
so rather than going to a url i need it to run a javascript function
You didn't specify what javascript function you wanted to run or what the function is supposed to do.

Getting your script to run a function instead of the command location.href='url' is rather easy.

But I see that you are originally loading the location.href from a PHP function.
So I am a little confused as to what it is you are trying to accomplish.

But no matter, here is how you run a javascript function instead of a command.

Example:
HTML Code:
<html>
<head>
<script language="javascript">
function init_keyboard_events()
{
 if(document.all){document.onkeypress = handler;}
 else{window.onkeypress = handler;}
 }
 
function handler(e)
{
 var x = document.all ? window.event.keyCode : e.charCode;
 if(x == 119){do_something();}
 }
 
function do_something()
{
 alert("Doing something.");
 }
</script>
</head>
<body onload="init_keyboard_events();">
</body>
</html>
__________________
Jerry Broughton

Last edited by job0107; 06-22-09 at 12:06 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 06-22-09, 04:29 AM
smithygotlost smithygotlost is offline
Coding Addict
 
Join Date: Jul 2006
Location: United Kingdom
Posts: 335
Thanks: 2
Thanked 0 Times in 0 Posts
sorry sorry sorry my bad the function it needs to run is

Code:
<script language="javascript">
<!--

var state = 'none';

function showhide(layer_ref) {

if (state == 'block') {
state = 'none';
stopkeys(false);
}
else {
state = 'block';
stopkeys(true);
}
if (document.all) { //IS IE 4 or 5 (or 6 beta)
eval( "document.all." + layer_ref + ".style.display = state");
}
if (document.layers) { //IS NETSCAPE 4 or below
document.layers[layer_ref].display = state;
}
if (document.getElementById &&!document.all) {
hza = document.getElementById(layer_ref);
hza.style.display = state;
}
}
//-->
var xmlhttp

function charmove(roomid,hex)
{
moveplayer(roomid,hex);
createworld();
getmobs(roomid);
}
var mobsoff = false;
function mobtoggle() {
if (mobsoff == false) {
  mobsoff = true;
    document.getElementById("getmobs").innerHTML = "";
} else {
  mobsoff = false;
}
}
function getmobs(roomid) {
  if (mobsoff == true) {} else {
	send_url = "getmobs.php?roomid="+roomid;
	mobsrequester = createRequester();
	mobsrequester.onreadystatechange = function(){
		if(mobsrequester.readyState == 4 && mobsrequester.status == 200){
			document.getElementById("getmobs").innerHTML = mobsrequester.responseText;
		}
	}

	mobsrequester.open("GET", send_url);

	if(OS == 'IE'){
		mobsrequester.send();
	}
	else{
		mobsrequester.send(null);
	}
  }
}
function createworld() {
	send_url = "new_createworld.php";
	requester = createRequester();
	requester.onreadystatechange = function(){
		if(requester.readyState == 4 && requester.status == 200){
			document.getElementById("createworld").innerHTML = requester.responseText;
		}
	}

	requester.open("GET", send_url);

	if(OS == 'IE'){
		requester.send();
	}
	else{
		requester.send(null);
	}
}
function moveplayer(roomid,hex) {
	send_url = "moveplayer.php?roomid="+roomid+"&hex="+hex;
	moverequester = createRequester();
	moverequester.open("GET", send_url);

	if(OS == 'IE'){
		moverequester.send();
	}
	else{
		moverequester.send(null);
	}
}
</script>
i need it to run

Code:
charmove($map_link_up,'$hex_up')
every time the w is pressed however your script allows me to get the alert just not run this function, any ideas

Thanks
Mike
__________________
Make People Friendly Say " Thanks "
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Hotkey Help smithygotlost JavaScript 3 01-13-07 07:29 PM
Its posibble emulate keypress event? fermdp JavaScript 1 10-11-06 08:48 PM
Keypress mimic mouse click tim8w Visual Basic 1 12-07-04 02:55 PM


All times are GMT -5. The time now is 08:43 PM.
vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.