TL DR: How to redefine the functionality of Windows, or at least perform an action BEFORE this happens? It seems that global hooks are not fast enough to happen before
I have a problem with some code I'm working on ... I'm trying to override the natural behavior of Windows when I press CTRL + V, instead of pasting from the clipboard, I would, for example, replace the contents of the clipboard with the contents of my program designations.
NOTE THIS NEEDS TO BE APPLIED IN EXISTING ACTIONS OUTSIDE MY PROGRAM, WELL, THE PRESENT USE OF GLOBAL HOOKS
below is a snippet of how I am currently trying to βinterceptβ the paste hot keys and explain why this does not work:
Here I define my global hooks:
class globalKeyboardHook {
#region Constant, Structure and Delegate Definitions
public delegate int keyboardHookProc(int code, int wParam, ref keyboardHookStruct lParam);
public struct keyboardHookStruct {
public int vkCode;
public int scanCode;
public int flags;
public int time;
public int dwExtraInfo;
}
const int WH_KEYBOARD_LL = 13;
const int WM_KEYDOWN = 0x100;
const int WM_KEYUP = 0x101;
const int WM_SYSKEYDOWN = 0x104;
const int WM_SYSKEYUP = 0x105;
#endregion
#region Instance Variables
public List<Keys> HookedKeys = new List<Keys>();
IntPtr hhook = IntPtr.Zero;
#endregion
#region Events
public event KeyEventHandler KeyDown;
public event KeyEventHandler KeyUp;
#endregion
#region Constructors and Destructors
public globalKeyboardHook() {
hook();
}
~globalKeyboardHook() {
unhook();
Dispose();
}
#endregion
#region Public Methods
public void hook() {
IntPtr hInstance = LoadLibrary("User32");
hhook = SetWindowsHookEx(WH_KEYBOARD_LL, hookProc, hInstance, 0);
}
public void Dispose()
{
try { unhook(); }
catch (Exception e)
{ }
}
public void unhook() {
UnhookWindowsHookEx(hhook);
}
public int hookProc(int code, int wParam, ref keyboardHookStruct lParam) {
if (code >= 0) {
Keys key = (Keys)lParam.vkCode;
if (HookedKeys.Contains(key)) {
KeyEventArgs kea = new KeyEventArgs(key);
if ((wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) && (KeyDown != null)) {
KeyDown(this, kea) ;
} else if ((wParam == WM_KEYUP || wParam == WM_SYSKEYUP) && (KeyUp != null)) {
KeyUp(this, kea);
}
if (kea.Handled)
return 1;
}
}
return CallNextHookEx(hhook, code, wParam, ref lParam);
}
#endregion
#region DLL imports
[DllImport("user32.dll")]
static extern IntPtr SetWindowsHookEx(int idHook, keyboardHookProc callback, IntPtr hInstance, uint threadId);
[DllImport("user32.dll")]
static extern bool UnhookWindowsHookEx(IntPtr hInstance);
[DllImport("user32.dll")]
static extern int CallNextHookEx(IntPtr idHook, int nCode, int wParam, ref keyboardHookStruct lParam);
[DllImport("kernel32.dll")]
static extern IntPtr LoadLibrary(string lpFileName);
#endregion
}
And here is the main question:
globalKeyboardHook gkh = new globalKeyboardHook();
bool Vpressed;
bool controlIsUp;
void gkh_KeyUp(object sender, KeyEventArgs e)
{
if(e.KeyCode.ToString()=="LControlKey" || e.KeyCode.ToString()=="RControlKey")
{
if(Vpressed && !controlIsUp)
{
Clipboard.SetDataObject(myTextLine);
Vpressed=false;
controlIsUp = true;
}
}
if (e.KeyCode.ToString() == "V")
{
if(controlIsUp)Vpressed=false;
else Vpressed=true;
}
}
void gkh_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode.ToString()=="LControlKey" || e.KeyCode.ToString()=="RControlKey")
{
controlIsUp=false;
}
}
So I see that the system pastes what is on the clipboard before pressing the call "Clipboard.setDataObjec (...)" when I want the contents of the clipboard to SET before the system pastes them.
Also, my next step is to detect # times when the V key is pressed before CTRL is released, and insert another message based on this (for example, CTL + V inserts "HI FRIEND", CTRL + V + V paste "TEST TEST 2 ', and CTRL + V + V + V inserts the "third fragment")
, : , , ? - / , , CTRL + V? , "" (.. , , ), , ?