13 Replies - 5481 Views - Last Post: 04 September 2012 - 08:09 AM Rate Topic: -----

#1 giuseppe105   User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 448
  • Joined: 15-May 08

[Solved]paste stored data to different app

Posted 03 September 2012 - 11:34 AM

EDIT read changes at bottom.

Hello everyone its been a while.

I want to make an application that will paste a defined line of text to any window within windows.
But I'm at a loss of if or how my idea will work.

From what iv gathered so far. My idea is to use the Global hot key call from user32.dll to set global hot keys.

from there I would take the current contents of the windows clipboard and store it in a variable. I would then take the text from a text box and put it inside the windows clipboard. then i would somehow force windows to paste the text from the clipboard into a window something like notepad or a web browser or Microsoft word.

The only idea I have to forcing a paste would be to have my program do key presses.

I'm using curtis rutlands tutorial on GlobalKeyPress http://www.dreaminco...global-hotkeys/
And this clipboard tutorial http://www.geekpedia...ith-Csharp.html

my main concerns are

1. how do I make my program force paste regardless of what window has focus.

EDIT:

OK I got the program to paste text to notepad and other windows too. But i have 2 problems.
1. I can only use the hot key 1 time. the only way to make it work again is to change the window focus.
2. I need more then 1 global hot key, How do I determine witch one was pressed?

Here is my code
private GlobalHotKey ghk1;

        public frmMemoryCP()
        {
            InitializeComponent();
            ghk1 = new GlobalHotKey(Constants.CTRL + Constants.ALT, Keys.D1, this);
        }

        private void HandleHotkey()
        {
            WriteLine("Hotkey pressed!");
            Clipboard.SetText(txtGhk1.Text);
            SendKeys.Send("^V");
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == Constants.WM_HOTKEY_MSG_ID)
                HandleHotkey();
            base.WndProc(ref m);
        }

Any help would be greatly appreciated
Thank you

This post has been edited by giuseppe105: 03 September 2012 - 08:21 PM


Is This A Good Question/Topic? 0
  • +

Replies To: [Solved]paste stored data to different app

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6932
  • View blog
  • Posts: 23,569
  • Joined: 05-May 12

Re: [Solved]paste stored data to different app

Posted 03 September 2012 - 01:27 PM

View Postgiuseppe105, on 03 September 2012 - 11:34 AM, said:

2. I need more then 1 global hot key, How do I determine witch one was pressed?


You'll have to crack the LPARAM to see what keys were being held down: See MSDN: http://msdn.microsof...9(v=vs.85).aspx

As well as one of the comments in posted in Curtis' tutorial that you linked to.
Was This Post Helpful? 1
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6932
  • View blog
  • Posts: 23,569
  • Joined: 05-May 12

Re: [Solved]paste stored data to different app

Posted 03 September 2012 - 01:33 PM

Why are you sending Ctrl-V to paste? What if I've remapped my text editors keys so that Ctrl-V deletes all my text. Now your hotkey has just screwed up what I'me currently working on.

Have you considered sending a WM_PASTE message instead? http://msdn.microsof...8(v=vs.85).aspx
Was This Post Helpful? 0
  • +
  • -

#4 giuseppe105   User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 448
  • Joined: 15-May 08

Re: [Solved]paste stored data to different app

Posted 03 September 2012 - 04:34 PM

EDIT AT BOTTOM

Thank you for the post Skydriver. after I get the multi hot key working I'm going to attempt to use WM_PASTE that you mentioned.

Yes the first reply to the curtis tutorial showed how to detect multiple hot keys but it doesn't work.

here the code I'm using. most of it is copied.
private Keys GetKey(IntPtr LParam)
        {
            return (Keys)((LParam.ToInt32()) >> 16);
        }

        private void HandleHotKey(int num)
        {
            if(num == 1)
            {
                WriteLine("Hotkey 1 pressed!");
                Clipboard.SetText(txtGhk1.Text);
                SendKeys.Send("^V");
            }
            else if (num == 2)
            {
                WriteLine("Hotkey 2 pressed!");
                Clipboard.SetText(txtGhk2.Text);
                SendKeys.Send("^V");
            }
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == Constants.WM_HOTKEY_MSG_ID)
                switch (GetKey(m.LParam))
                {
                    case Keys.D1:
                        HandleHotKey(1);
                        break;
                    case Keys.D0:
                        HandleHotKey(2);
                        break;
                }
            base.WndProc(ref m);
        }


The first hot key works but the second one wont work. Any Ideas?

Sorry guys i made a horrible mistake. Due to lack any number of excuses i forgot to register and unregister the second hot key.

Now lets see if i can figure out how to use WM_PASTE i think it might solve the only being able to use the hot key once problem.

This post has been edited by giuseppe105: 03 September 2012 - 04:45 PM

Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6932
  • View blog
  • Posts: 23,569
  • Joined: 05-May 12

Re: [Solved]paste stored data to different app

Posted 03 September 2012 - 05:22 PM

Seems to work correctly for me...
Spoiler


Of course, I haven't tried the numeric keypad keys since my laptop doesn't have them that I can get to without some major finger gymnastics. The code above, though, does Alt-Shift for O, X, F1 and F2 without any issues.
Was This Post Helpful? 0
  • +
  • -

#6 giuseppe105   User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 448
  • Joined: 15-May 08

Re: [Solved]paste stored data to different app

Posted 03 September 2012 - 05:42 PM

Thank you for the post skydriver the hotkeys work perfectly fine its a problem with SendKeys. I have to find a different way to fire the paste event. I am trying SendMessage right now and im getting alot of trouble. I also found this project but i have no idea how to add a dll to my project.
http://inputsimulator.codeplex.com/

anyway this is my send message code.
I added WM_PASTE to my Constants.cs
private IntPtr holdme = ((IntPtr)0);

SendMessage(this.Handle, Constants.WM_PASTE, holdme, holdme);

[DllImport("user32.dll")]
        private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);


Using this code does not paste the data from the clipboard to the notepad document

holdme is just my lazy way to make an intPtr 0 MSDN said that WM_PASTE needed wParam and lParam to be 0

This post has been edited by giuseppe105: 03 September 2012 - 05:45 PM

Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6932
  • View blog
  • Posts: 23,569
  • Joined: 05-May 12

Re: [Solved]paste stored data to different app

Posted 03 September 2012 - 05:48 PM

I just realized... D0 and D1 just the numbers along the top of the keyboard. Adding these lines in the constructor correctly reports the hotkeys being when they go to the default case in the switch statement:
new GlobalHotkey(Constants.ALT + Constants.SHIFT, Keys.D0, this).Register();
new GlobalHotkey(Constants.ALT + Constants.SHIFT, Keys.D1, this).Register();


Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6932
  • View blog
  • Posts: 23,569
  • Joined: 05-May 12

Re: [Solved]paste stored data to different app

Posted 03 September 2012 - 05:54 PM

View Postgiuseppe105, on 03 September 2012 - 05:42 PM, said:

anyway this is my send message code.
I added WM_PASTE to my Constants.cs
private IntPtr holdme = ((IntPtr)0);

SendMessage(this.Handle, Constants.WM_PASTE, holdme, holdme);

[DllImport("user32.dll")]
        private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);


Using this code does not paste the data from the clipboard to the notepad document

holdme is just my lazy way to make an intPtr 0 MSDN said that WM_PASTE needed wParam and lParam to be 0


That is because you are using this.Handle as the target of the SendMessage(), you need to have the window handle of the textbox or RichEdit control, that you want to paste into.
Was This Post Helpful? 0
  • +
  • -

#9 giuseppe105   User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 448
  • Joined: 15-May 08

Re: [Solved]paste stored data to different app

Posted 03 September 2012 - 06:11 PM

I replaced
this.Handle
with
[DllImport("user32.dll")]
        public static extern IntPtr GetForegroundWindow();
GetForegroundWindow()


from reading around it said something about selecting a child context handler like the edit menu.

Im so lost...
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6932
  • View blog
  • Posts: 23,569
  • Joined: 05-May 12

Re: [Solved]paste stored data to different app

Posted 03 September 2012 - 06:52 PM

GetForegroundWindow() isn't going to cut it because all that will give you is the main window or dialog that has the input focus. It will not forward WM_PASTE messages to the window that has the input focus. You need the window handle of the window of the control that you want to paste into.

Perhaps WM_PASTE may not be a very good option you. Just be aware that if somebody has text editor in vi mode, or some other emulation mode where the standard CUA Ctrl-C,V,X shortcuts are enabled, you won't be able to paste into them.
Was This Post Helpful? 0
  • +
  • -

#11 giuseppe105   User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 448
  • Joined: 15-May 08

Re: [Solved]paste stored data to different app

Posted 03 September 2012 - 06:59 PM

I understand that there will be trouble if i use the send keys method.

But send keys only works once. I need it to work every time i press the hotkey. I'm testing with notepad.

Somewhere on the internet i read somthing about journal hooking but i have no idea what that is.

Shkydriver can you show me a different method then sendKeys to press control + V.

Iv tryed kbpress_event.

and i cant understand sendInput.
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6932
  • View blog
  • Posts: 23,569
  • Joined: 05-May 12

Re: [Solved]paste stored data to different app

Posted 03 September 2012 - 07:20 PM

I think that it's the choice of modifier keys that is screwing you up. If I have ALT as a modifier, then pastes does nothing all. If I use other modifiers, notepad is happy.

    public partial class Form1 : Form
    {
        public Form1()
        {
            // InitializeComponent();

            // The following do not work:

            new GlobalHotkey(Constants.ALT + Constants.SHIFT, Keys.F1, this).Register();
            new GlobalHotkey(Constants.ALT, Keys.F2, this).Register();

            // The following work:

            new GlobalHotkey(Constants.SHIFT, Keys.F3, this).Register();
            new GlobalHotkey(Constants.CTRL, Keys.F4, this).Register();
            new GlobalHotkey(Constants.WIN, Keys.F5, this).Register();
            new GlobalHotkey(Constants.NOMOD, Keys.F6, this).Register();
        }

        private void HandleHotkey(long lParam)
        {
            lParam >>= 16;
            Keys key = (Keys)lParam;

            switch (key)
            {
                case Keys.F1:
                    Clipboard.SetText("Alt+Shift-F1!");
                    SendKeys.Send("^V");
                    break;

                case Keys.F2:
                    Clipboard.SetText("Alt-F2!");
                    SendKeys.Send("^V");
                    break;

                case Keys.F3:
                    Clipboard.SetText("Shift-F3!");
                    SendKeys.Send("^V");
                    break;

                case Keys.F4:
                    Clipboard.SetText("Ctrl-F4!");
                    SendKeys.Send("^V");
                    break;

                case Keys.F5:
                    Clipboard.SetText("Win-F5!");
                    SendKeys.Send("^V");
                    break;

                case Keys.F6:
                    Clipboard.SetText("F6!");
                    SendKeys.Send("^V");
                    break;
            }
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == Constants.WM_HOTKEY_MSG_ID)
                HandleHotkey((long)m.LParam);
            base.WndProc(ref m);
        }
    }


This post has been edited by Skydiver: 03 September 2012 - 07:21 PM

Was This Post Helpful? 1
  • +
  • -

#13 giuseppe105   User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 448
  • Joined: 15-May 08

Re: [Solved]paste stored data to different app

Posted 03 September 2012 - 07:42 PM

Woooooooooooooooow. I cant believe my eyes.

Your a GENIUS!

This app is meant to be used in adobe reader.

It looks like CTRL + d1 ~ 0 are for changing the view.
But it beats losing Shift + D1 ~ 0

Thanks a million for helping me skydriver.

I was about to copy and paste a giant amount of code to use sendInput. that might not have even worked.

+1+1+1+1+!
Was This Post Helpful? 0
  • +
  • -

#14 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: [Solved]paste stored data to different app

Posted 04 September 2012 - 08:09 AM

Glad you guys figured it out. I refined my Hooks/HotKeys code a long time ago and put the updates here:

https://github.com/c...d/LowLevelHooks

Check out the updated hotkey code and the updated hotkey test project. They make it a bit easier to get all the information about a pressed hotkey.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1