keypress - How can I get rid of character repeat delay in C#? -
- There is an option in Windows to install repeat delay. This means that the delay between the first keystroke and others keeps one key pressed and I'm making a type of game and I need to get rid of this "feature".
So far I have The method has been able to find:
[DllImport ("user32.dll")] Use stable extern GetAsyncKeyState (int vKey); Public static bool IsKeyPushedDown (key data) {return 0! = (GetAsyncKeyState (int) key data) and 0x8000); }
But the method IsKeyPushedDown finds that the key is pressed at the speed of calling the function - so I need a loop to check if the key is down The problem is that it is now Also does not catch all keystrokes - I have a very slow loop.
The second option is overriding the ProcessMDK:
Protected Override Bull ProcessCMK (Ref Message Message, Keyage Data) {// Processing Keys (If Key Data == Key.lift || keydata == key.wright || key data == key.upa || key data == key.down) {return true; } Else {return base.ProcessCmdKey (riff message, keydata); }}
This really works well, but it is affected with repeating and so the speed of my monster in my game is the same:
Is the solution to the problem? Thanks
Edit: I have solved the problem with the combination of both processes but this is a very ugly solution. I still hope that there is a better solution
You should deal with keydown
and key up
instead of ProcessCmdKey
Want more control over what happens during pressing and releasing the key.
The simplest thing to do in your scenario is that the timer that moves the monster on your form when KeyDown
is removed, enable the timer (and variable Or set DeltaX and Delta which indicates how to move the monster), then stop the timer when KeyUp
is removed.
edit
as an example (very barebones)
public class MyForm: form {Private Int DeltaX; Private Int Delta; Private contact with ArtaultAmount = 10; Private Timer Movement Timer = New Timer (); Public MyForm () {movementTimer.Interval = 100; // Let's go between the movements in whatever interval you want. Timer Tick + = new event heredollar (movement timercut); } Zero movement timer (object sender, event ergges e) {myMonster.Location = new point (myMonster.X + Deltax, MyMostar.Y + Delta); } Safe Override Zero OnKeyDown (KeyEventArgs e) {base.OnKeyDown (E); Switch (E. cacode) {Case keys. Lift: {deltaX - = movement standard; } break; Of the case. Correct: {deltaX + = movement amount; } break; Case key Up: {deltaY - = movement amount; } break; Case key.Down: {delta + = movement amount; } break; } Update timer (); } Secure Override Zero OnKeyUp (KeyEventArgs e) {base.OnKeyUp (E); Switch (E. cacode) {Case Keys. Lift: {deltaX + = movement; } break; Of the case. Correct: {deltaX - = movement amount; } break; Case key Up: {Deltae = = Amount of movement; } break; Case key Download: {delta - = movement amount; } break; } Update timer (); } Private Zero UpdateTimer () {movementTimer.Enabled = deltaX! = 0 || Delta! = 0; }}
Comments
Post a Comment