Elegant way to parse Clipboard content following a mouse event

0

I'm trying to find an elegant way to parse the Clipboard content following a mouse event in C#.

I'm currently creating a tool in C# WinForm for the game "PathOfExile" that would basically do the following:

On mouse down over an item
    if the item contain any desirable mod
        Disable the mouse//suppress the mouse click

To do so, I created a small winform project using "MouseKeyHook" and "WindowsInput" in order to capture mouse/keyevent and send the ctrl+c.

In PathOfExile, when you send a "Ctrl+C" while your cursor is on top of an item, all the item's properties are copied to the clipboard.

I was hoping not having to multithread the tool since the process should work as follow:

Mouse down

send ctrl+c

Copy the clipboard content

Analyze the clipboard

continue with the mouse events, or suppress the mouse down

While doing the above, I have some trouble with the Clipboard where I don't receive the information from the latest "CTRL+C". It looks like I'm receiving the "previous" clipboard information. Also, it seems that when I subscribe to "OnMouseDownExt", I'm not able to open Chrome anymore... But this might be an issue with the MouseKeyHook pluging.

Here is my piece of code:

private IKeyboardMouseEvents m_Events;

  private WindowsInput.InputSimulator iSim = new InputSimulator();

  private void SubscribeGlobal()
  {
     Unsubscribe();
     Subscribe(Hook.GlobalEvents());
  }

  private void Subscribe(IKeyboardMouseEvents events)
  {
     m_Events = events;
     m_Events.MouseDownExt += OnMouseDownExt;
  }

  private void Unsubscribe()
  {
     if (m_Events == null) return;
     m_Events.MouseDownExt -= OnMouseDownExt;

     m_Events.Dispose();
     m_Events = null;
  }


  private void OnMouseDownExt(object sender, MouseEventExtArgs e)
  {

 // Send the CTRL+C 
 iSim.Keyboard.ModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_C);

 string strClipboard = "Clipboard empty";

 for (int i = 0; i < 10; i++)
 {
    try
    {
       if (Clipboard.ContainsText())
       {
          strClipboard = Clipboard.GetText();
          Clipboard.Clear();
          break;
       }
    }
    catch (Exception ex)
    {
       Log(ex.ToString());
       //fix for OpenClipboard Failed (Exception from HRESULT: 0x800401D0 (CLIPBRD_E_CANT_OPEN))
       //https://stackoverflow.com/questions/12769264/openclipboard-failed-when-copy-pasting-data-from-wpf-datagrid
       //https://stackoverflow.com/questions/68666/clipbrd-e-cant-open-error-when-setting-the-clipboard-from-net
       System.Threading.Thread.Sleep(10);
    }

 }


 Log(strClipboard);

  }

  private void Log(string text)
  {
     if (IsDisposed) return;
     textBoxLog.Text = text;

  }

Let me know if you ever had similar issues.

Thanks!

c#
clipboard
mousekeyhook
asked on Stack Overflow Feb 19, 2021 by LazyCrafter

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0