QuestionHelpDo something on key press

Posts 111 of 11 · Page 1 of 1
Do something on key press
Hi, i am new to C# so i need your help
I want to code something that should happen when i press a key on my keyboard, to make it clear:
I want to make a FOV change for mw3, everytimes i press the + or - button on my numkeyboard the FOV should change.
My question is not how to change the FOV but how to do that this happens everytimes i press + or - while beeing ingame?
Hope someone understands my question and can help me, sry for my bad english :P
Paschga
GetKeyState API

Code:
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern short GetKeyState(Keys nVirtKey);
A little function for your hotkeys
Code:
        public bool Keystate(Keys key)
        {
            int St = GetKeyState(key);
            return (St == -127 || St == -128);
        }
Store the variable
Code:
float FOV = 0;
Create a timer and within it you would put this
Code:
            if (Keystate(Keys.Add) | Keystate(Keys.Oemplus))
            {
                FOV += .5f;
            }
            if (Keystate(Keys.Subtract) | Keystate(Keys.OemMinus))
            {
                FOV -= .5f;
            }
Something like that should work
Use GetAsyncKeyState, put it inside a loop or a timer. GetAsyncKeyState finds if a keys is pressed or was pressed since the last call.

PInvoke:
Code:
[DllImport("user32.dll")]
static extern short GetAsyncKeyState(System.Windows.Forms.Keys vKey);
Usage:

Code:
if(Convert.ToBoolean(GetAsyncKeyState(Keys.PageUp)) // Don't remember the + and - code hehe
        //Increase FOV here
You don't really need the Convert.ToBoolean in fact.. you can do like if (GetAsyncKeyState(Keys.PageUp) != 0). And, again, put it on a timer.




Quote Originally Posted by MarkHC View Post
Use GetAsyncKeyState, put it inside a loop or a timer. GetAsyncKeyState finds if a keys is pressed or was pressed since the last call.

PInvoke:
Code:
[DllImport("user32.dll")]
static extern short GetAsyncKeyState(System.Windows.Forms.Keys vKey);
Usage:

Code:
if(Convert.ToBoolean(GetAsyncKeyState(Keys.PageUp)) // Don't remember the + and - code hehe
        //Increase FOV here
You don't really need the Convert.ToBoolean in fact.. you can do like if (GetAsyncKeyState(Keys.PageUp) != 0). And, again, put it on a timer.




Tip to make it easier for code:

Make the return value of the static extern short GetAsyncKeyState(System.Windows.Forms.Keys vKey);
Do like this: static extern bool GetAsyncKeyState(System.Windows.Forms.Keys vKey);

Then you just do:
Code:
if(GetAsyncKeyState(Keys.PageUp))
FoV++; //Or what you use to increase your FoV with.
Thank you for the fast answers, now i have a second question, i'm playing the game in borderless window mode, how high should i set the 'timer.interval' that my game does not lagg? i have a real bad pc...
And is there a possibility(i already set the FoV change code to a button) that everytimes i click + or - C# simply clicks the button?
What loop would you use?
Thank you again for helping me
Quote Originally Posted by Paschga View Post
Thank you for the fast answers, now i have a second question, i'm playing the game in borderless window mode, how high should i set the 'timer.interval' that my game does not lagg? i have a real bad pc...
Play with the value and you'll see
Quote Originally Posted by Paschga View Post
Thank you for the fast answers, now i have a second question, i'm playing the game in borderless window mode, how high should i set the 'timer.interval' that my game does not lagg? i have a real bad pc...
And is there a possibility(i already set the FoV change code to a button) that everytimes i click + or - C# simply clicks the button?
What loop would you use?
Thank you again for helping me
It has nothing to do imo, just try to set the process priority to low and it should accept any kind of value
After some tryes i figured out that i have no idea what i was doing, can someone give me the whole script for the button1_click?
i have no idea how i should code a loop... shame on me...
Thank you all for your work

---------- Post added 07-08-2012 at 12:12 AM ---------- Previous post was 07-07-2012 at 10:46 PM ----------

thank you all, i got it myself you are the best!
Good you made it.
I hope you used a Timer.
And not: While | For loops.

If you need help with anything else just let us know.
We are around
yes, i used a timer, i was so confused when i tried it with a loop, it always stuck and got in an infinite wheel.
Then i tried the timer and everything was so easy thanks
If you want to run the script in a command button I have always moved the code into its own void and just called the void in the button so that it still functioned.
Posts 111 of 11 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?