使用 windows 全局键盘挂钩获取 ctrl+v 文件夹路径

get ctrl+v folder path with windows global keyboard hooking

我用 C# 编写了一个全局键盘挂钩应用程序。我可以按下 Ctrl+v 但我不知道 Ctrl+[= 的文件夹路径14=]v 按下。我怎么才能得到它?这是我的代码:

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;
        }
    }
}

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.

编辑:

在这里我找到了一些有用的代码: C# Get the Windows Explore Path which has the focus

如果没有资源管理器window(期望默认资源管理器)设置桌面路径。

Environment.GetFolderPath(Environment.SpecialFolder.Desktop)

当按下 Ctrl+v 时,我使用以下代码获取当前 windows 路径:

    [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;
    }

您必须添加以下参考;

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

您可以在这里找到 Win32 实现; Win32 Implementation Sample