0

So i'm writing a little tool to have a bit easier access to functions because there are no hotkeys.

For now i use global hotkeys to move my mouse to specific locations on the monitor and perform r/l clicks.

KeyboardHook hook = new KeyboardHook();

hook.KeyPressed +=
new EventHandler<KeyPressedEventArgs>(hook_KeyPressed);

hook.RegisterHotKey(TestBox.ModifierKeys.Control, Keys.D1);

void hook_KeyPressed(object sender, KeyPressedEventArgs e)
        {
            Process[] p = Process.GetProcessesByName(".......");
            if (p.Count() > 0)
                SetForegroundWindow(p[0].MainWindowHandle);
            RightMouseClick(2000,110);
            System.Threading.Thread.Sleep(200);
            LeftMouseClick(2060,120);
        }

So far so good. Now my problem:

The coordinates are the ones from my right monitor (triple setup). But what if i decide to switch the programm to my left monitor? Then the coordinates wont match anymore. My way would be something like this, but i don't know how to start and implement this.

If you first press the hotkey, there is a check for coordinates. If not -> ask to set them. User then moves mouse to first point and press F2, move to second point and press F2 again. So the coordinates get saved and the hotkey can be performed.

  • 1
    Do you want to "click" a certain fixed location within some other window? You might try [finding where the window for that process is](https://stackoverflow.com/q/1434524/2821583), and storing the offset from that window's position. This might break if the window resizes, but it may work if the window simply moves. – Jeremy Feb 13 '23 at 06:49
  • Setup a different hotkey that allows the user to set the position to target based on the current mouse position. Use [WindowFromPoint()](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-windowfrompoint) to get the target window. Then pass that handle to [GetAncestor()](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getancestor?source=recommendations) with `GA_ROOT` to get the main window handle. Now you can use the aforementioned `GetWindowRect()` to get the coords of the main window. The "Offset" to the click point can now be calculated. – Idle_Mind Feb 13 '23 at 15:45

1 Answers1

0

So i figured out a solution for my needs. Just a bit of a messy solution, but it works.

I addressed another hotkey. When the user moves the mouse to a specific point and press it, the coordinates will be inserted into the textboxes.

txt_disc_right_x.Text = MousePosition.X.ToString();
txt_disc_right_y.Text = MousePosition.Y.ToString();
void disconnectright_KeyPressed(object sender, KeyPressedEventArgs e)
        {
            int disc_rightx = int.Parse(txt_disc_right_x.Text);
            int disc_righty = int.Parse(txt_disc_right_y.Text);
            int disc_leftx = int.Parse(txt_disc_left_x.Text);
            int disc_lefty = int.Parse(txt_disc_left_y.Text);
            
            if (box_disconnectright.Checked)
            {
                Process[] p = Process.GetProcessesByName(".......");
                if (p.Count() > 0)
                    SetForegroundWindow(p[0].MainWindowHandle);
                RightMouseClick(disc_rightx,disc_righty);
                System.Threading.Thread.Sleep(300);
                LeftMouseClick(disc_leftx,disc_lefty);
            }
        }

In case of restarting the programm, the values will be saved into a textfile and stored locally.

With this solution, i dont rely on a specific layout and everyone can move their programms like they want and can still use all the hotkeys.

Example of my UI