0

The program is working at background and listening keyboard(like keylogger) First I select a string on pdf or etc. Then I pressed the ctrl + rbutton the program must be pop up and it can be get my selected string.

For this;

[DllImport("User32.dll")]
private static extern short GetAsyncKeyState(System.Windows.Forms.Keys vKey);

[DllImport("User32.dll")]
private static extern short GetAsyncKeyState(System.Int32 vKey);

string key = "";
private void timer1_Tick(object sender, EventArgs e)
{
    timer1.Interval = 5;
    foreach (System.Int32 i in Enum.GetValues(typeof(Keys)))
    {
        int x = GetAsyncKeyState(i);
        if ((x == 1) || (x == -32767))
        {
            keyBuffer += Enum.GetName(typeof(Keys), i);
        }
    }
    if (keyBuffer != "")
    {
        keyBuffer = keyBuffer.ToLower();
        if (keyBuffer.Contains("lcontrolkeyrbutton"))
        {
            // do somethings
            keyBuffer = "";
        }
    }
}

But after first performing, ctrl + rbutton it doesn't work. What's the wrong? And how can i get the selected string into my program?

axmed2004
  • 113
  • 1
  • 4
miqbal
  • 2,213
  • 3
  • 27
  • 35

4 Answers4

1

Sounds like you want a clipboard hook (in addition to your global key hook). Take a look here: Clipboard event C#

If you want to capture the string without the user copying it into the clipboard manually, you could send ctrl+c to the application yourself: http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx

Community
  • 1
  • 1
Ohad Schneider
  • 36,600
  • 15
  • 168
  • 198
1

Here is the program funcional and tested

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CtrLetterCopy
{

    public partial class Form1 : Form
    {
        [DllImport("user32.dll")]
        private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
        [DllImport("user32.dll")]
        private static extern bool UnregisterHotKey(IntPtr hWnd, int id);

        const int WM_COMMAND = 0x111;

        enum KeyModifier
        {
            None = 0,
            Alt = 1,
            Control = 2,
            Shift = 4,
            WinKey = 8
        }

        public Form1()
        {
            InitializeComponent();
            this.ShowInTaskbar = false;
            int id_Ctrl = 0;     // The id of the hotkey.
            RegisterHotKey(this.Handle, id_Ctrl, (int)KeyModifier.Control, Keys.R.GetHashCode());
        }

        protected override void WndProc(ref Message m)
        {
            //const int WM_HOTKEY = 0x0312;
            if (m.Msg == 0x0312)
            {
                if (m.WParam.ToInt32() == 0)
                {
                    //do what you want here
                    SendKeyEvent();

                }
            }
            base.WndProc(ref m);
        }

        private void SendKeyEvent()
        {
            SendKeys.SendWait("^c");
            Thread.Sleep(500);
            string test3 = Clipboard.GetText();
            MessageBox.Show(test3);            
        } 
    }
}
1

You need to SendKeyEvent(Ctrl+C) of MainWindowsHandle First, then Using Clipboard capture your text to your Project.

0

Download and check out this code here:

http://www.codeproject.com/KB/cs/globalhook.aspx

Pre-tested, well-written and fully functional.

Best of luck!

Flipster
  • 4,373
  • 4
  • 28
  • 36