Quantcast
Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
    1. Welcome to GTAForums!

    2. News

    1. Red Dead Redemption 2

      1. Gameplay
      2. Missions
      3. Help & Support
    2. Red Dead Online

      1. Gameplay
    1. GTA Online

      1. After Hours
      2. Find Lobbies & Players
      3. Guides & Strategies
      4. Vehicles
      5. Content Creator
      6. Help & Support
    2. Crews

      1. Events
      2. Recruitment
    1. Grand Theft Auto Series

    2. GTA Next

    3. GTA V

      1. PC
      2. Guides & Strategies
      3. Help & Support
    4. GTA IV

      1. Episodes from Liberty City
      2. Multiplayer
      3. Guides & Strategies
      4. Help & Support
      5. GTA Mods
    5. GTA Chinatown Wars

    6. GTA Vice City Stories

    7. GTA Liberty City Stories

    8. GTA San Andreas

      1. Guides & Strategies
      2. Help & Support
      3. GTA Mods
    9. GTA Vice City

      1. Guides & Strategies
      2. Help & Support
      3. GTA Mods
    10. GTA III

      1. Guides & Strategies
      2. Help & Support
      3. GTA Mods
    11. Top Down Games

      1. GTA Advance
      2. GTA 2
      3. GTA
    12. Wiki

      1. Merchandising
    1. GTA Modding

      1. GTA V
      2. GTA IV
      3. GTA III, VC & SA
      4. Tutorials
    2. Mod Showroom

      1. Scripts & Plugins
      2. Maps
      3. Total Conversions
      4. Vehicles
      5. Textures
      6. Characters
      7. Tools
      8. Other
      9. Workshop
    3. Featured Mods

      1. DYOM
      2. OpenIV
      3. GTA: Underground
      4. GTA: Liberty City
      5. GTA: State of Liberty
    1. Red Dead Redemption

    2. Rockstar Games

    1. Off-Topic

      1. General Chat
      2. Gaming
      3. Technology
      4. Programming
      5. Movies & TV
      6. Music
      7. Sports
      8. Vehicles
    2. Expression

      1. Graphics / Visual Arts
      2. GFX Requests & Tutorials
      3. Writers' Discussion
      4. Debates & Discussion
    1. Forum Support

    2. Site Suggestions

ClareXoBearrx3R9

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

Recommended Posts

ClareXoBearrx3R9

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. :)

 

 

 

 

 

Share this post


Link to post
Share on other sites
Jitnaught

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

Share this post


Link to post
Share on other sites
Skorpro

Maybe it's interesting for you:

 

b8 IsPlayerPressingHorn(Player playerIndex)

if (IsPlayerPressingHorn(GetPlayer()))

Share this post


Link to post
Share on other sites
Jitnaught

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

Share this post


Link to post
Share on other sites
ClareXoBearrx3R9

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

Share this post


Link to post
Share on other sites
Skorpro

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);}

Share this post


Link to post
Share on other sites
ClareXoBearrx3R9

 

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.

Share this post


Link to post
Share on other sites
Skorpro

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"...

Share this post


Link to post
Share on other sites
ClareXoBearrx3R9

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

Share this post


Link to post
Share on other sites
Skorpro

 

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 :)

Share this post


Link to post
Share on other sites
jobnocky71

You are giving me the horn!

Share this post


Link to post
Share on other sites

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

  • 1 User Currently Viewing
    0 Members, 0 Anonymous, 1 Guest

×

Important Information

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