ClareXoBearrx3 Posted January 23, 2015 Share Posted January 23, 2015 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. Link to comment https://gtaforums.com/topic/763328-performing-methods-while-holding-key-down-instead-of-press-rele/ Share on other sites More sharing options...
Jitnaught Posted January 23, 2015 Share Posted January 23, 2015 (edited) 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 January 23, 2015 by LetsPlayOrDy ClareXoBearrx3 1 Link to comment https://gtaforums.com/topic/763328-performing-methods-while-holding-key-down-instead-of-press-rele/#findComment-1066869543 Share on other sites More sharing options...
Skorpro Posted January 23, 2015 Share Posted January 23, 2015 Maybe it's interesting for you: b8 IsPlayerPressingHorn(Player playerIndex) if (IsPlayerPressingHorn(GetPlayer())) Link to comment https://gtaforums.com/topic/763328-performing-methods-while-holding-key-down-instead-of-press-rele/#findComment-1066870130 Share on other sites More sharing options...
Jitnaught Posted January 23, 2015 Share Posted January 23, 2015 (edited) 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 January 23, 2015 by LetsPlayOrDy Link to comment https://gtaforums.com/topic/763328-performing-methods-while-holding-key-down-instead-of-press-rele/#findComment-1066870193 Share on other sites More sharing options...
ClareXoBearrx3 Posted January 23, 2015 Author Share Posted January 23, 2015 (edited) 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 January 23, 2015 by ClareXoBearrx3 Link to comment https://gtaforums.com/topic/763328-performing-methods-while-holding-key-down-instead-of-press-rele/#findComment-1066870334 Share on other sites More sharing options...
Skorpro Posted January 24, 2015 Share Posted January 24, 2015 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);} Link to comment https://gtaforums.com/topic/763328-performing-methods-while-holding-key-down-instead-of-press-rele/#findComment-1066870782 Share on other sites More sharing options...
ClareXoBearrx3 Posted January 24, 2015 Author Share Posted January 24, 2015 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. Link to comment https://gtaforums.com/topic/763328-performing-methods-while-holding-key-down-instead-of-press-rele/#findComment-1066870796 Share on other sites More sharing options...
Skorpro Posted January 24, 2015 Share Posted January 24, 2015 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"... GRAX 1 Link to comment https://gtaforums.com/topic/763328-performing-methods-while-holding-key-down-instead-of-press-rele/#findComment-1066870978 Share on other sites More sharing options...
ClareXoBearrx3 Posted January 24, 2015 Author Share Posted January 24, 2015 (edited) 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! [Edit]: Just tried this and it worked perfectly! Edited January 25, 2015 by ClareXoBearrx3 Link to comment https://gtaforums.com/topic/763328-performing-methods-while-holding-key-down-instead-of-press-rele/#findComment-1066874375 Share on other sites More sharing options...
Skorpro Posted February 2, 2015 Share Posted February 2, 2015 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! [Edit]: Just tried this and it worked perfectly! You're welcome ClareXoBearrx3 1 Link to comment https://gtaforums.com/topic/763328-performing-methods-while-holding-key-down-instead-of-press-rele/#findComment-1066910433 Share on other sites More sharing options...
jobnocky71 Posted March 4, 2015 Share Posted March 4, 2015 You are giving me the horn! Link to comment https://gtaforums.com/topic/763328-performing-methods-while-holding-key-down-instead-of-press-rele/#findComment-1067055621 Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now