0

I wrote an global keyboard hook application in c#. I can catch Ctrl+v pressed But I don't know folder path that Ctrl+v pressed on. How can I get it? This is my code:

public partial class Form1 : Form {

    private globalKeyboardHook gkh{get;set;} 
    private bool ctrl { get; set; }

    public Form1() {
        InitializeComponent();

        gkh = new globalKeyboardHook();
        ctrl = false;
    }

    private void Form1_Load(object sender, EventArgs e) {
        gkh.HookedKeys.Add(Keys.V);
        gkh.HookedKeys.Add(Keys.LControlKey);
        gkh.HookedKeys.Add(Keys.RControlKey);
        gkh.KeyDown += new KeyEventHandler(gkh_KeyDown);
        gkh.KeyUp += new KeyEventHandler(gkh_KeyUp);
    }

    void gkh_KeyUp(object sender, KeyEventArgs e) {
        lstLog.Items.Add("Up\t" + e.KeyCode.ToString());

        if (e.KeyCode == Keys.LControlKey || e.KeyCode == Keys.RControlKey) ctrl = false;
    }

    void gkh_KeyDown(object sender, KeyEventArgs e) {
        lstLog.Items.Add("Down\t" + e.KeyCode.ToString());          

        if (e.KeyCode == Keys.LControlKey || e.KeyCode == Keys.RControlKey) {
            ctrl = true;
        } else if (ctrl && e.KeyCode == Keys.V){
            ctrl = false;

            //do Paste operation

            e.Handled = true;
        }
    }
}
Mwiza
  • 7,780
  • 3
  • 46
  • 42
doğan
  • 339
  • 1
  • 4
  • 15
  • I can get copied or cutted object from clipboard, but i can't destination folder path to transfer items when I press ctrl + v. – doğan Apr 11 '16 at 12:56

2 Answers2

1

I think it is impossible, but you can try to compare files before and after ctrl+v.

After that you will get the files and destinations that were changed.

EDIT:

Here i found some code that will be useful: C# Get the Windows Explore Path which has the focus

If there isn't any explorer window ( expect default one ) set the path to desktop.

Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Community
  • 1
  • 1
  • I am trying to prevent windows paste operation to make it with my own copy application. So when i catch ctrl + v There is no change on the file system. – doğan Apr 11 '16 at 15:23
  • some type of virus I guess :) ? –  Apr 11 '16 at 15:28
  • No it isn't a virus :). I am trying to made an application like teracopy, ultracopier, vs. On the other hands, thanks for the answer. It is working for me but there is a little problem; while any window is open, if I press ctrl+v on desktop it gets the open window path, not desktop path. This is last problem of me :). – doğan Apr 12 '16 at 07:37
  • Check if file with a specific name exists after ctrl+v in a window path. if not it's propably on desktop ;) –  Apr 12 '16 at 15:53
  • But it can be chrome explorer, vlc Player, or something like this. – doğan Apr 13 '16 at 11:23
  • I fixed it comparing return value of GetForegroundWindow() and return value of Win32.GetDesktopWindow(Win32.DesktopWindow.ProgMan) function. If it is same i return Desktop path. – doğan Apr 13 '16 at 14:58
  • You are not writing a virus, but note that you maybe wiil have to convince the antivirus running on destination PC that's not a virus. Many antivirus complain when a program tries to install keyboard hooks. Also note that CTRL+V is not the only method to paste in Exploer. Right click -> paste can be used (I'm lazy, sometimes I don't want to move my hands from mouse to keyobord, and right click comes to my help. Surely I'm not the only lazy one around...) – Gian Paolo Nov 20 '17 at 10:57
0

When Ctrl+v pressed I get current windows path with following code:

    [DllImport("user32.dll")]
    static extern IntPtr GetForegroundWindow();

    private string getFocusedWindowPath() {
        IntPtr handle = GetForegroundWindow();
        IntPtr desktop = Win32.GetDesktopWindow(Win32.DesktopWindow.ProgMan);

        SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();
        foreach (SHDocVw.InternetExplorer window in shellWindows) {
            if ((int)handle == (int)desktop) {
                return Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            } else if (window.HWND == (int)handle) {
                return new Uri(window.LocationURL).LocalPath;
            }
        }
        return null;
    }

You must add following ref;

  1. C:\Windows\system32\Shell32.dll
  2. C:\Windows\system32\ShDocVw.dll

You can find Win32 implamentation here; Win32 Implementation Sample

Mwiza
  • 7,780
  • 3
  • 46
  • 42
doğan
  • 339
  • 1
  • 4
  • 15