-1

I want to make a WPF program which converts unit (numbers from other applications). I made my program can run at system tray and I can set global hotkey too. But it's my first time making a WPF program so I'm having some problems.

This function is called when user presses global hotkey. To calculate, I need to copy/paste selected text from other application. I would appreciate it if you help me. (The code might be a little messy)

  • My user wants automatically convert his selected text.
  • He is using my application like this now: 1. Select text 2. Press Ctrl+C 3. Press Ctrl+D(Global Hotkey) 4. Press Ctrl+V.
  • What he wants is: 1. Select text 2. Press Ctrl+D(Global Hotkey) and it's done.

    void ClipboardCalc()
    {
        string resultStr = string.Empty;
        string temp = string.Empty;
        string digit = string.Empty;
        double result = 0;
        temp = Clipboard.GetText();
    
        // Need to copy "selected text from other application" to clipboard here.
    
        resultStr = Clipboard.GetText();
        resultStr = string.Join(string.Empty, Regex.Match(resultStr, @"\d+(\.\d+)?"));
        try
        {
            result = Convert.ToDouble(resultStr);
        } 
        catch (Exception ee)
        {
        }
        if (reverse_mode == false)
            result *= 3.30579;
        else
            result /= 3.30579;
        digit = "F" + textBox2.Text;
    
        if (result == (int)result)
            resultStr = result.ToString();
        else
            resultStr = result.ToString(digit);
        resultStr = string.Format("{0:0.##########}", Convert.ToDouble(resultStr));
    
        if (print_measure != false)
        {
            if (reverse_mode != false)
                resultStr += "평";
            else
                resultStr += "m²";
        }
        Clipboard.SetText(resultStr);
    
        // Then paste clipboard text here.
    
        //Clipboard.SetText(temp);
    }
    
  • https://stackoverflow.com/questions/3546016/how-to-copy-data-to-clipboard-in-c-sharp – Prateek Shrivastava Mar 19 '20 at 22:26
  • @PrateekShrivastava I already used Clipboard.SetText() at the code above. The point is to copy text from other applications. – soyoon3292 Mar 19 '20 at 22:59
  • _"...The point is to copy text from other applications..."_ - It's probably not polite for one application to cause another application to suddenly send stuff to the clipboard. Data sensitivity and all that –  Mar 19 '20 at 23:29
  • @soyoon3292 - You cant copy text from Other application without the other application supporting that fuctionality. Its like I write an app and say I want to copy something that appears in soyoon3292's app. Without you opening that feature in your app, my code is going nowhere. – Prateek Shrivastava Mar 19 '20 at 23:32
  • @PrateekShrivastava there's no way to command ctrl+c and ctrl+v? – soyoon3292 Mar 20 '20 at 07:02
  • @MickyD Yes but my user wants automatically convert his selected text.. He is using my application like this now: 1. Select text 2. Press Ctrl+C 3. Press Ctrl+D(Global Hotkey) 4. Press Ctrl+V. What he wants is: 1. Select text 2. Press Ctrl+D(Global Hotkey) and it's done. – soyoon3292 Mar 20 '20 at 07:33
  • Take a look at Microsoft UI Automation –  Mar 20 '20 at 09:19

1 Answers1

0

This is a bodge sort of answer, but it seems like a bodge sort of program, so I'll put it out there anyway.

If you've managed to set up a "global hotkey", then I'm assuming you've already gotten into p/invoke territory. So I might as well introduce you to SendInput. This native method lets an application "synthesize keystrokes, mouse motions, and button clicks to the currently active window". You can use it to send Ctrl+C to the active window.

Again, a bodged solution with a lot of assumptions, but it will probably work in most cases.

Note that you'll have to send key down and key up inputs individually (i.e. "Ctrl down", "C down", "C up", "Ctrl up", in that order).

Keith Stein
  • 6,235
  • 4
  • 17
  • 36
  • As no one answered to my question, I solved by myself using System.Windows.Forms.SendKeys.SendWait("^{c}") and ("^{v}"). I also tried using Send/PostMessage() with WM_KEYDOWN/KEYUP too. I learned a lot by myself reading MSDN. Thank you for the answer :D – soyoon3292 Mar 27 '20 at 06:48