Clipboard.GetText() - 返回旧值

时间:2017-09-01 11:10:48

标签: c# wpf

我正在创建一个程序,使用户能够复制和粘贴不同的“Makros”。所以你按Ctrl-1表示第一个配置文件,Ctrl-2表示第二个配置文件,依此类推...我将复制的数据保存在string[]内,用Ctrl-1我将currentIndex设置为0和将此值(storage[0])添加到剪贴板。多数民众赞成我希望它如何运作。 以下是代码:

    bool ctrl = false;
    bool number = false;

    int currentIndex = 0;
    string[] storage = new string[9];


    private LowLevelKeyboardListener _listener;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void FRM_main_Loaded(object sender, RoutedEventArgs e)
    {
        _listener = new LowLevelKeyboardListener();
        _listener.OnKeyPressed += _listener_OnKeyPressed;
        _listener.HookKeyboard();
    }

    void _listener_OnKeyPressed(object sender, KeyPressedArgs e)
    {
        if (e.KeyPressed == Key.LeftCtrl)
        {
            ctrl = true;
        }

        if (e.KeyPressed == Key.D1)
        {
            if (ctrl == true)
            {
                currentIndex = 0;
                number = true;
                if (storage[currentIndex] != null)
                {
                    Clipboard.SetDataObject(storage[currentIndex].ToString());
                }
            }
        }

        if (e.KeyPressed == Key.D2)
        {
            if (ctrl == true)
            {
                currentIndex = 1;
                number = true;
                if (storage[currentIndex] != null)
                {
                    Clipboard.SetDataObject(storage[currentIndex].ToString());
                }
            }
        }

        if (e.KeyPressed == Key.C)
        {
            if (ctrl == true && number == true)
            {
                Thread.Sleep(1000);
                storage[currentIndex] = Clipboard.GetText();
            }
        }

        TB_log.Text += "\n\n";
        TB_log.Text += "Storage1: " + storage[0] + "\n";
        TB_log.Text += "Storage2: " + storage[1] + "\n";
        TB_log.Text += "Clipboard: " + Clipboard.GetText() + "\n";
        TB_log.Text += "Pressed Key: " + e.KeyPressed.ToString();
    }

    private void FRM_main_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        _listener.UnHookKeyboard();
    }

以下是LowLevelKeyboardListener:From Here

class LowLevelKeyboardListener
{
    private const int WH_KEYBOARD_LL = 13;
    private const int WM_KEYDOWN = 0x0100;
    private const int WM_SYSKEYDOWN = 0x0104;

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool UnhookWindowsHookEx(IntPtr hhk);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr GetModuleHandle(string lpModuleName);

    public delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);

    public event EventHandler<KeyPressedArgs> OnKeyPressed;

    private LowLevelKeyboardProc _proc;
    private IntPtr _hookID = IntPtr.Zero;

    public LowLevelKeyboardListener()
    {
        _proc = HookCallback;
    }

    public void HookKeyboard()
    {
        _hookID = SetHook(_proc);
    }

    public void UnHookKeyboard()
    {
        UnhookWindowsHookEx(_hookID);
    }

    private IntPtr SetHook(LowLevelKeyboardProc proc)
    {
        using (Process curProcess = Process.GetCurrentProcess())
        using (ProcessModule curModule = curProcess.MainModule)
        {
            return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
        }
    }

    private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
    {
        if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_SYSKEYDOWN)
        {
            int vkCode = Marshal.ReadInt32(lParam);

            if (OnKeyPressed != null) { OnKeyPressed(this, new KeyPressedArgs(KeyInterop.KeyFromVirtualKey(vkCode))); }
        }

        return CallNextHookEx(_hookID, nCode, wParam, lParam);
    }
}

public class KeyPressedArgs : EventArgs
{
    public Key KeyPressed { get; private set; }

    public KeyPressedArgs(Key key)
    {
        KeyPressed = key;
    }
}

问题:

  1. 我按Ctrl-1选择个人资料1
  2. 然后我标记“word-a”并按Ctrl-C
  3. 我按Ctrl-2选择个人资料2
  4. 然后我标记“word-b”并按Ctrl-C
  5. 再次按Ctrl-1选择个人资料1
  6. 和Ctrl-V - 果然,“word-a”被粘贴了
  7. 按Ctrl-2选择个人资料2
  8. 并按Ctrl-V - “word-b”粘贴了
  9. 到目前为止一切顺利。

    现在,当我切换回Profile 1并按Ctrl-C时, 标记的单词“word-c”在我的剪贴板中。我现在可以粘贴它。但是 - “word-c”就在我的剪贴板中,而不在storage[0]里。 我指的是这一行: Thread.Sleep(1000); storage[currentIndex] = Clipboard.GetText(); 我认为没有理由不再这样做了。

    在新的WPF项目中自己尝试一下。只需创建一个TextBox(TB_log)即可查看复制的内容以及storage内的内容,并按照说明进行操作。

0 个答案:

没有答案