Gamestudio Links
Zorro Links
Newest Posts
Zorro 2.70
by jcl. 09/29/25 09:24
optimize global parameters SOLVED
by dBc. 09/27/25 17:07
ZorroGPT
by TipmyPip. 09/27/25 10:05
assetHistory one candle shift
by jcl. 09/21/25 11:36
Plugins update
by Grant. 09/17/25 16:28
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
1 registered members (TipmyPip), 18,619 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
krishna, DrissB, James168, Ed_Love, xtns
19168 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 2 of 2 1 2
Re: Capturing Keyboard Input [Re: indiGLOW] #216740
07/18/08 15:14
07/18/08 15:14
Joined: Feb 2006
Posts: 2,185
mpdeveloper_B Offline
Expert
mpdeveloper_B  Offline
Expert

Joined: Feb 2006
Posts: 2,185
it seems overly complex to me. You really didn't need to use all those variables for defining keys and stuff. If you want, here's mine, it's for A6, but I'm pretty sure it would be easy to convert if you have A7:

Note: these functions already include support for all keys on an american keyboard (that 3DGS will allow) and the mouse buttons.

Code:
string keystr[50];
string tmpkey[50];
string oldkey[50];

//these are string definitions for your movement keys, these are what you would 
//use in the menu as the strings for the keys to be shown
string up_str = "Up"; 
string left_str = "Left";
string right_str = "Right";
string down_str = "Down";
///these are the variables you will use with the press_key(num) function
var_info up = 72;
var_info left = 75;
var_info right = 77;
var_info down = 80;

///if you want to add support for more keys that aren't working, add them here
function set_keyname(keystr)
{
	if (key_lastpressed <= 255) //if the scancode is lessthan or equal to 255
	{
		str_for_key (keystr,key_lastpressed);
		if (key_lastpressed == 14)
		{
			str_cpy (keystr, "backspace");
		}
		if (key_lastpressed == 42)
		{
			str_cpy (keystr, "left shift");
		}
		if (key_lastpressed == 54)
		{
			str_cpy (keystr, "right shift");
		}
		if (key_lastpressed == 82)
		{
			str_cpy (keystr, "insert");
		}
		if (key_lastpressed == 83)
		{
			str_cpy (keystr, "delete");
		}
		if (key_lastpressed == 73)
		{
			str_cpy (keystr, "page up");
		}
		if (key_lastpressed == 81)
		{
			str_cpy (keystr, "page down");
		}
		if (key_lastpressed == 72)
		{
			str_cpy (keystr, "up arrow");
		}
		if (key_lastpressed == 80)
		{
			str_cpy (keystr, "down arrow");
		}
		if (key_lastpressed == 75)
		{
			str_cpy (keystr, "left arrow");
		}
		if (key_lastpressed == 77)
		{
			str_cpy (keystr, "right arrow");
		}
		if (key_lastpressed == 12)
		{
			str_cpy (keystr, "minus");
		}
		if (key_lastpressed == 43)
		{
			str_cpy (keystr, "backslash");
		}
		if (key_lastpressed == 39)
		{
			str_cpy (keystr, "semicolon");
		}
		if (key_lastpressed == 40)
		{
			str_cpy (keystr, "apostrophie");
		}
		if (key_lastpressed == 51)
		{
			str_cpy (keystr, "comma");
		}
		if (key_lastpressed == 52)
		{
			str_cpy (keystr, "period");
		}
		if (key_lastpressed == 70)
		{
			str_cpy (keystr, "scroll lock");
		}
	}
	else //if the scancode is more than 255, joystick buttons would be added here
	{
		if (key_lastpressed == 280)
		{
			str_cpy (keystr, "mouse left");
		}
	}
}

function reset_key(keystr, tmpkey)
{
	str_cpy(oldkey, tmpkey);
	recheck:
	while (key_any) { wait(1); }
	str_cpy (tmpkey, "Press A Key");
	mouse_mode = 0;
	//freeze_mode = 1;
	while (key_any == null) { wait(1); }
	if (!key_pressed(55) && !key_pressed(key_for_str("esc")) && !key_pressed(key_for_str("grave")))
	{
		str_for_num(keystr, key_lastpressed);
		set_keyname (tmpkey);
		while (key_any) { wait(1); }
		mouse_mode = 1;
		return;
		//freeze_mode = 0;
	}
	if (key_pressed(key_for_str("esc")))
	{
		str_cpy(tmpkey, oldkey); return;
	}
	str_cpy (tmpkey, "Invalid Key");
	sleep(1);
	goto(recheck);
}

function press_key(num)
{
	return(key_pressed(num));
}



now, for the way you use it. First of all, to change the key that is mapped, do this:

reset_key(key_variable, key_string);

so, for the up key that would be:

reset_key(up, up_str);

what this will do is wait for you to press a key to map it to, if you press esc it quits the function and resets the key to the old one. ~ will just return a "incorrect key" because most people use this for a console, if you don't like this just erase the && !key_pressed(key_for_str("grave")) from the reset_key function.

now, to use the keys in the game, just do this:

if (press_key(key_variable)) { ///blahblahblah }

for up that will be:

if (press_key(up)) { ///blahblahblah }

you could replace your:

if(keycode == k_moveForward){CamEnt.camforce_x += 1;}

commands with:

if (press_key(up)) {CamEnt.camforce_x += 1;}

like I said, you can use any key that 3DGS allows, all you have to do to enable them for the string is add the keys you want to allow in the set_keyname(keystr) function, you don't use this function yourself, so all you need to do is add keys here, i'll show how to add joystick 1 button 1:

joystick 1 button 1 scan code is 256, so under this line:

else //if the scancode is more than 255, joystick buttons would be added here
{
if (key_lastpressed == 280)
{
str_cpy (keystr, "mouse left");
}
if (key_lastpressed == 256)
{
str_cpy (keystr, "joy1 button1"); // you could put whatever name you want here
}
}

I hope this helps laugh


- aka Manslayer101
Re: Capturing Keyboard Input [Re: mpdeveloper_B] #216794
07/18/08 20:30
07/18/08 20:30
Joined: Oct 2003
Posts: 1,550
United Kingdom
indiGLOW Offline OP
Serious User
indiGLOW  Offline OP
Serious User

Joined: Oct 2003
Posts: 1,550
United Kingdom
Thanks for this, with your blessing I would like to migrate this code into my application. If appropriate I will add you to the credits.

I found that I had headed down a specific route which turned out to be a blind alley.

I am confident that when I sit down and include this tomorrow that I will be able to move forward again. LOL accidental pun intended smile

Thanks again


The Art of Conversation is dead : Discuss
Re: Capturing Keyboard Input [Re: indiGLOW] #216827
07/18/08 23:23
07/18/08 23:23
Joined: Feb 2006
Posts: 2,185
mpdeveloper_B Offline
Expert
mpdeveloper_B  Offline
Expert

Joined: Feb 2006
Posts: 2,185
sure, no credit needed although I wouldn't be mad if I were added laugh


- aka Manslayer101
Re: Capturing Keyboard Input [Re: mpdeveloper_B] #217030
07/21/08 00:16
07/21/08 00:16
Joined: Dec 2005
Posts: 116
T
tD_Datura_v Offline
Member
tD_Datura_v  Offline
Member
T

Joined: Dec 2005
Posts: 116
Hm. Interesting.
This useless bit is some sort of sick derivative.
Ignore it.
Code:
/*
multiple evals after match and assign in set_keyname? 
*/

/*
parallel arrays
*/
var in_atos1[512];
var in_aton1[512];
var in_aact1[32];



TEXT in_t1 {
	strings = 50;
}

var in_n1 = 0;
var in_n2 = 0;
var in_nLines1 = 0;
STRING in_s1;

function inf_t1(_n1, _s1) {
	if (in_nLines1 >= in_t1.strings) {
		error("err: in_t1.strings exceeded!");
		return(0);
	}
	in_atos1[_n1] = in_nLines1;
	in_aton1[in_nLines1] = _n1;
	str_cpy(in_t1.string[in_nLines1], _s1);
	
	in_nLines1+=1;
	return(1);
}
function inf_new1() {
	in_n1 = 0;
	inf_t1(12, "minus");
	inf_t1(14, "backspace");
	inf_t1(39, "semicolon");
	inf_t1(40, "apostrophe");
	inf_t1(42, "left shift");
	inf_t1(43, "backslash");
	inf_t1(51, "comma");
	inf_t1(52, "period");
	inf_t1(54, "right shift");
	inf_t1(70, "scroll lock");
	inf_t1(72, "up arrow");
	inf_t1(73, "page up");
	inf_t1(77, "right arrow");
	inf_t1(80, "down arrow");
	inf_t1(81, "page down");
	inf_t1(82, "insert");
	inf_t1(83, "delete");
	inf_t1(256, "joy1 button1");
	inf_t1(280, "mouse left");
}
// inf_s1(82);
// in_s1 is "insert"
function inf_s1(_nCode) {
	in_n2 = in_atos1[_nCode];
	str_cpy(in_s1, in_t1.string[in_n2]);
}

// inf_n1("insert") is 82
function inf_n1(_sName) {
	in_n1 = 0;
	if (_sName == NULL) { return(-1); }
	while(in_n1 < in_nLines1) {
		if (str_cmpni(in_t1.string[in_n1], _sName)) { 
			return(in_aton1[in_n1]);
		}
		in_n1 += 1;
	}
	return(-1);
}

// tested to here

var act_run = 0;
var in_nAct1 = 0;

function inf_set1(&_nAct, _s1) {
	_nAct[0] = in_nAct1;
	in_aact1[_nAct] = inf_n1(_s1);
	in_nAct1 += 1;
}
/*
inf_set1(act_run, "left shift");
*/
function inf_h(_nAct) {
	return(key_pressed(in_aact1[_nAct]));
}
/*
if (inf_h(act_run)) {
*/


Quote:
I found that I had headed down a specific route which turned out to be a blind alley.

Yes, that's probably a bad case of wall redistribution.
Of course, your route planner could have faults.

Re: Capturing Keyboard Input [Re: tD_Datura_v] #217458
07/23/08 09:14
07/23/08 09:14
Joined: Oct 2003
Posts: 1,550
United Kingdom
indiGLOW Offline OP
Serious User
indiGLOW  Offline OP
Serious User

Joined: Oct 2003
Posts: 1,550
United Kingdom
I can confirm this code worked great and with a little modification I was able to fully integrate it with my code.

I now have working key mapping with save/load ini file.

Thanks again

@ td_Datura_V: LOL, dont you just hate wall re-distrabution?!?


The Art of Conversation is dead : Discuss
Page 2 of 2 1 2

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1