Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The following code holds keys and releases it
[DllImport("user32.dll", SetLastError = true)]
        static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

        public const int KEYEVENTF_KEYUP = 0x02;
        public const int VK_W = 0x57;

        private void timer1_Tick(object sender, EventArgs e)
        {
            keybd_event(VK_W, 0, 0, 0);
            // keybd_event(VK_W, 0, KEYEVENTF_KEYUP, 0);
        }



This does work but not in Minecraft. After a hour of headache I found that I can't call minecraft defined controls. for example if I had 'Walk Foward' key set to 'W' I can't call that key programmitcaly unless I change that key to something else. But there are other key pressing applications that works with games, how they do it? Is there any other API that is capable of calling and holding keys?

What I have tried:

Tested the code on other applications works fine.
Posted
Comments
johannesnestler 3-Dec-18 7:52am    
sounds strange, are you shure there is no other problem (missing focus or something like that?)
AFAIK keybd_event was replaced with SendInput (Maybe read the docu remarks for SendInput https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-sendinput)
Thaana Paana 25-Dec-18 12:40pm    
I know I'm very late but I think its java, and I actually gave up on this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900