Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
429 views
in Technique[技术] by (71.8m points)

c# - Issue with simulating right click using PostMessage

Im developing a program that is simulating mouse events for Minecraft. I want to simulate right clicks with delay when holding right button but its working when I'm holding left button instead of right.

I made a class for right clicks:

class RightMouseHelper
    {
        const int WM_RBUTTONUP = 0x0205;
        const int WM_RBUTTONDOWN = 0x0204;
        public static bool GetMouseState()
        {
            return GetKeyState(0x001) > 1;
        }
        public static void PostMessage(int mouseright, IntPtr handleright)
        {
            if (mouseright == 0) PostMessage(handleright, WM_RBUTTONUP, 0x01, 0);
            else PostMessage(handleright, WM_RBUTTONDOWN, 0x01, 1);
        }

        [DllImport("user32.dll")]
        static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

        [DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern int GetKeyState(int vKey);
    }

And in the main class i did this:

if (AsyncKeyHelper.isKeyDown(Keys.F8))
                    {
                        toggledright = !toggledright;
                        Thread.Sleep(100);
                    }

Task.Run(() =>
            {
                while (true)
                {
                    while (RightMouseHelper.GetMouseState() && toggledright)
                    {
                        if (++counter > 20 && foregroundProcess != null && foregroundProcess.Id == process.Id) 
                        {
                            Task.Run(() =>
                            {
                                RightMouseHelper.PostMessage(0, process.MainWindowHandle);
                                Thread.Sleep(90);
                                RightMouseHelper.PostMessage(1, process.MainWindowHandle);
                            });
                            Thread.Sleep(350);
                        }
                    }
                   

                    Thread.Sleep(5);
                }
            });


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...