private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (_tmpIntercept && nCode >= 0)
{
int vkCode = Marshal.ReadInt32(lParam);
// Debug($"wParam = {wParam}, lParam = {lParam}, vkCode = {vkCode}");
if (wParam == (IntPtr)WM_KEYUP)
{
switch ((Keys)vkCode)
{
case Keys.LShiftKey:
case Keys.RShiftKey:
_shiftPressed = false;
Debug("Shift Key released");
break;
case Keys.LControlKey:
case Keys.RControlKey:
_ctrlPressed = false;
Debug("Ctrl Key released");
break;
case Keys.LWin:
case Keys.RWin:
//_windowsPressed = false;
Debug("Windows Key released");
break;
}
}
else if (wParam == (IntPtr)WM_KEYDOWN)
{
switch ((Keys)vkCode)
{
case Keys.LShiftKey:
case Keys.RShiftKey:
_shiftPressed = true;
Debug("Shift Key pressed");
break;
case Keys.LControlKey:
case Keys.RControlKey:
_ctrlPressed = true;
Debug("Ctrl Key pressed");
break;
case Keys.LWin:
case Keys.RWin:
//_windowsPressed = true;
Debug("Windows Key pressed");
break;
case Keys.Escape:
_replaceText = false;
Stopped?.Invoke(this, EventArgs.Empty);
Debug("Stop replacing text");
break;
case Keys.F1:
case Keys.F2:
case Keys.F3:
case Keys.F4:
case Keys.F5:
case Keys.F6:
case Keys.F7:
case Keys.F8:
case Keys.F9:
case Keys.F10:
case Keys.F11:
case Keys.F12:
if (_ctrlPressed && _shiftPressed)
{
_replaceText = true;
_textIndex = vkCode - 112;
_replIndex = 0;
_tmpIntercept = true;
Started?.Invoke(this, new IntEventArgs(_textIndex));
Debug("Start replacing text : " + AutoTypedText[_textIndex]);
}
break;
default:
if (_ctrlPressed && _shiftPressed && (Keys)vkCode == Keys.Up)
{
// Ctrl-Shift-Up increases the number of letters typeds
this.NbOfLettersTyped++;
}
else if (_ctrlPressed && _shiftPressed && (Keys)vkCode == Keys.Down)
{
// Ctrl-Shift-Down decreases the number
this.NbOfLettersTyped--;
}
else if (_replaceText)
{
bool lettersTyped = false;
for (int i = 0; i < this.NbOfLettersTyped; i++)
{
if (_replIndex < AutoTypedText[_textIndex].Length)
{
lettersTyped = true;
_tmpIntercept = false;
string keys = string.Empty;
switch (AutoTypedText[_textIndex][_replIndex])
{
case '{':
case '}':
case '(':
case ')':
case '%':
case '+':
case '^':
case '~':
keys = string.Format("{{{0}}}", AutoTypedText[_textIndex][_replIndex]);
break;
default:
keys = AutoTypedText[_textIndex][_replIndex].ToString();
break;
}
Debug($"Replace capture by '{keys}'. Index = {_replIndex}");
SendKeys.Send(keys);
_replIndex++;
_tmpIntercept = true;
KeyStroke?.Invoke(this, new IntEventArgs(_replIndex));
}
else
{
_replaceText = false;
Stopped?.Invoke(this, EventArgs.Empty);
Debug("Stop replacing text : no more input data");
}
}
if (lettersTyped)
{
// Cancel current keystroke
Debug("Return (IntPtr)1");
return (IntPtr)1;
}
}
break;
}
}
}
return CallNextHookEx(_hookId, nCode, wParam, lParam);
}