Jump to content

Performing Methods while Holding Key Down (Instead of Press & Rele


Recommended Posts

ClareXoBearrx3

Hello all,

 

So while coding, undoubtedly the most commonly used "method" to invoke something in your mod with a keypress of the A key on the keyboard (in C++) is by using something along the lines of:

 

SHORT key_myKey = GetAsyncKeyState(VkKeyScan('a'));if (key_myKey & 0x8000){  //  //}
I have no issues with this. In fact, assuming non-system keys, the 0x8000 according to the TechNet documentation indicates that a key is pressed.
The thing is, one of the methods I have is supposed to sound a vehicle's horn and as such, I want to do it only when the key is held down such that when released, the horn stops. According to TechNet documentation, a 0x0101 is broadcasted once a non-system key is released. For some reason, however, it doesn't work as it should. It could be my algorithm, but I wanted to check and see if this if 0x0101 is indeed the correct value to use for listening for a key release.
My algorithm (in C++) is something along the lines of:
b8 hornEnabled = false;SHORT key_myKey = GetAsyncKeyState(VkKeyScan('a'));// Activate horn when key is pressed.if ((key_myKey & 0x8000) && (hornEnabled == false)){    Vehicle v;    if (IsCharInAnyCar(GetPlayerPed()){        GetCarCharIsUsing(GetPlayerPed(),&v);        hornEnabled = true;        activateVehicleHorn(v, true); // True turns car horn on.    }}// Stop horn when key is released.if ((key_myKey & 0x0101) && (hornEnabled)){    Vehicle v;    if (IsCharInAnyCar(GetPlayerPed()){        GetCarCharIsUsing(GetPlayerPed(),&v);        hornEnabled = false;        activateVehicleHorn(v, false); // False turns car horn off.    }}

So my question: Is 0x0101 the correct value for listening to a key release? If so, does it appear that my algorithm would not work?

 

Thanks in advance. :)

 

 

 

 

 

You should be able to do just

bool keyPressed = (GetAsyncKeyState(0x41) & 1) != 0; //0x41 == aif (keyPressed && !hornEnabled) //!hornEnabled is the same thing as hornEnabled == false{   //pressed code}else if (!keyPressed && hornEnabled) //hornEnabled is the same thing as hornEnabled == true{  //not pressed code}
Edited by LetsPlayOrDy
  • Like 1

I don't think she's trying to get whether the horn is enabled or not. From the looks of her code, it looks like she is trying to set whether the horn is enabled or not.

Maybe making a remotely-controlled horn?

Edited by LetsPlayOrDy
ClareXoBearrx3

Thanks guys, for your help!

You should be able to do just

bool keyPressed = (GetAsyncKeyState(0x41) & 1) != 0; //0x41 == aif (keyPressed && !hornEnabled) //!hornEnabled is the same thing as hornEnabled == false{   //pressed code}else if (!keyPressed && hornEnabled) //hornEnabled is the same thing as hornEnabled == true{  //not pressed code}


^^ This actually looks like it would work (if used properly). So I tried that, as follows, yet it doesn't work :/
I'm actually trying to create a "manual" siren that can be turned on for the duration of holding the "." key and then stop when it's let go. My code:

b8 key_elss_sirenManual_pressed = GetAsyncKeyState(VkKeyScan('.') & 1) != 0;			// Manual Siren ON when key is pressed and held.			//if ((key_elss_sirenManual & 0x8000) && (elss_modeManualEnabled == true) && (elss_manualSirenActivated == false)){			if (key_elss_sirenManual_pressed && elss_modeManualEnabled && elss_manualSirenActivated){				Vehicle v;				elss_manualSirenActivated = true;				if(Scripting::IsCharInAnyPoliceVehicle(GetPlayerPed())){					Scripting::GetCarCharIsUsing(GetPlayerPed(),&v);					Scripting::SwitchCarSiren(v,true);				}				else{					PrintStringWithLiteralStringNow("STRING", "[Failed]: You are not in a police vehicle.", 1500, 1);				}			}			else if(!key_elss_sirenManual_pressed && elss_modeManualEnabled && elss_manualSirenActivated){				Vehicle v;				if(Scripting::IsCharInAnyPoliceVehicle(GetPlayerPed())){					Scripting::GetCarCharIsUsing(GetPlayerPed(),&v);					Scripting::SwitchCarSiren(v,false);					elss_manualSirenActivated = false;				}				else{					//PrintStringWithLiteralStringNow("STRING", "[Failed]: You are not in a police vehicle.", 1500, 1);				}			}

Perhaps I'm doing something wrong?

 

As a note:

> The variable "elss_modeManualEnabled" is true; this is later used to toggle between different siren modes.

> I am aware that I need not use "Scripting::" all the time, however, I do not remember all the native function names and Visual Studio is very helpful in reminding me with IntelliSense, as long as I put that prefix so that it knows what namespace to use as I'm typing ;)

Edited by ClareXoBearrx3

Hi,

this is just a hint... now it's on you to adapt this to your own code ;)

while(IsThreadAlive()){	if (GetAsyncKeyState(190) != 0) // "." key = 190 (German keyboard!)	{		PrintStringWithLiteralStringNow("STRING", "~g~Key pressed!", 100, 1);		Wait(0);	}	else if (GetAsyncKeyState(190) == 0)	{		PrintStringWithLiteralStringNow("STRING", "~r~Key NOT pressed!", 100, 1);		Wait(0);	}	Wait(0);}
ClareXoBearrx3

 

Hi,

this is just a hint... now it's on you to adapt this to your own code ;)

while(IsThreadAlive()){	if (GetAsyncKeyState(190) != 0) // "." key = 190 (German keyboard!)	{		PrintStringWithLiteralStringNow("STRING", "~g~Key pressed!", 100, 1);		Wait(0);	}	else if (GetAsyncKeyState(190) == 0)	{		PrintStringWithLiteralStringNow("STRING", "~r~Key NOT pressed!", 100, 1);		Wait(0);	}	Wait(0);}

 

Thanks! Although from what I can see, this is essentially what I have, so perhaps it's a algorithm error on my part. I'll look through my code once again.

You're welcome :)

 

Here is my try, I've added the siren now:

Vehicle veh;b8 bSiren = 0;//...while(IsThreadAlive()){	if (GetAsyncKeyState(190) != 0)	{		if (IsCharInAnyPoliceVehicle(GetPlayerPed()))		{			PrintStringWithLiteralStringNow("STRING", "~g~Siren ON!", 100, 1);			GetCarCharIsUsing(GetPlayerPed(), &veh);			SwitchCarSiren(veh, 1);			bSiren = 1;		}		Wait(0);	}	else if ( (GetAsyncKeyState(190) == 0) && (bSiren == 1) )	{		if (IsCharInAnyPoliceVehicle(GetPlayerPed()))		{			PrintStringWithLiteralStringNow("STRING", "~r~Siren OFF!", 100, 1);			GetCarCharIsUsing(GetPlayerPed(), &veh);			SwitchCarSiren(veh, 0);			bSiren = 0;		}		Wait(0);	}	Wait(0);}

Btw. your mistake is the first check of "elss_manualSirenActivated". It should be "!elss_manualSirenActivated"...

ClareXoBearrx3

You're welcome :)

 

Here is my try, I've added the siren now:

Vehicle veh;b8 bSiren = 0;//...while(IsThreadAlive()){	if (GetAsyncKeyState(190) != 0)	{		if (IsCharInAnyPoliceVehicle(GetPlayerPed()))		{			PrintStringWithLiteralStringNow("STRING", "~g~Siren ON!", 100, 1);			GetCarCharIsUsing(GetPlayerPed(), &veh);			SwitchCarSiren(veh, 1);			bSiren = 1;		}		Wait(0);	}	else if ( (GetAsyncKeyState(190) == 0) && (bSiren == 1) )	{		if (IsCharInAnyPoliceVehicle(GetPlayerPed()))		{			PrintStringWithLiteralStringNow("STRING", "~r~Siren OFF!", 100, 1);			GetCarCharIsUsing(GetPlayerPed(), &veh);			SwitchCarSiren(veh, 0);			bSiren = 0;		}		Wait(0);	}	Wait(0);}

Btw. your mistake is the first check of "elss_manualSirenActivated". It should be "!elss_manualSirenActivated"...

Ah, yes! I forgot to put an exclamation to negate the statement - totally forgot. Nice sharp eye you've got ;)

 

I'll definitely try this once I get back from work. Thanks again! :D

 

[Edit]: Just tried this and it worked perfectly!

Edited by ClareXoBearrx3
  • 2 weeks later...

 

Btw. your mistake is the first check of "elss_manualSirenActivated". It should be "!elss_manualSirenActivated"...

Ah, yes! I forgot to put an exclamation to negate the statement - totally forgot. Nice sharp eye you've got ;)

 

I'll definitely try this once I get back from work. Thanks again! :D

 

[Edit]: Just tried this and it worked perfectly!

 

You're welcome :)

  • Like 1
  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • 0 User Currently Viewing
    0 members, 0 Anonymous, 0 Guests

×
×
  • Create New...

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.