Witam, mam w swoim projekcie dodany timer który wywołuje metodę sprawdzającą wartość w adresie a także mam dodaną klasę globalhook która odpowiada za wykrywanie wciśnięcia klawisza na całym komputerze. Jednak w momencie gdy timer zostaje uruchomiony event odpowiadający za wciśnięcie klawisza kompletnie nie działa, czy jest możliwe wywołanie go asynchronicznie tak by jednocześnie działał timer i wywołanie eventa?
globalKeyboardHook class kod:
class globalKeyboardHook {
#region Constant, Structure and Delegate Definitions
/// <summary>
/// defines the callback type for the hook
/// </summary>
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;
//Modifier key vkCode constants
private const int VK_SHIFT = 0x10;
private const int VK_CONTROL = 0x11;
private const int VK_MENU = 0x12;
private const int VK_CAPITAL = 0x14;
#endregion
#region Instance Variables
/// <summary>
/// The collections of keys to watch for
/// </summary>
public List<Keys> HookedKeys = new List<Keys>();
/// <summary>
/// Handle to the hook, need this to unhook and call the next hook
/// </summary>
IntPtr hhook = IntPtr.Zero;
#endregion
#region Events
/// <summary>
/// Occurs when one of the hooked keys is pressed
/// </summary>
public event KeyEventHandler KeyDown;
/// <summary>
/// Occurs when one of the hooked keys is released
/// </summary>
public event KeyEventHandler KeyUp;
#endregion
#region Constructors and Destructors
/// <summary>
/// Initializes a new instance of the <see cref="globalKeyboardHook"/> class and installs the keyboard hook.
/// </summary>
public globalKeyboardHook() {
hook();
}
/// <summary>
/// Releases unmanaged resources and performs other cleanup operations before the
/// <see cref="globalKeyboardHook"/> is reclaimed by garbage collection and uninstalls the keyboard hook.
/// </summary>
~globalKeyboardHook() {
unhook();
}
#endregion
#region Public Methods
/// <summary>
/// Installs the global hook
/// </summary>
public void hook() {
IntPtr hInstance = LoadLibrary("User32");
hhook = SetWindowsHookEx(WH_KEYBOARD_LL, hookProc, hInstance, 0);
}
/// <summary>
/// Uninstalls the global hook
/// </summary>
public void unhook() {
UnhookWindowsHookEx(hhook);
}
/// <summary>
/// The callback for the keyboard hook
/// </summary>
/// <param name="code">The hook code, if it isn't >= 0, the function shouldn't do anyting</param>
/// <param name="wParam">The event type</param>
/// <param name="lParam">The keyhook event information</param>
/// <returns></returns>
public int hookProc(int code, int wParam, ref keyboardHookStruct lParam) {
if (code >= 0) {
Keys key = (Keys)lParam.vkCode;
if (HookedKeys.Contains(key))
{
// Get Modifiers
key = AddModifiers(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);
}
/// <summary>
/// Checks whether Alt, Shift, Control or CapsLock
/// is pressed at the same time as the hooked key.
/// Modifies the keyCode to include the pressed keys.
/// </summary>
private Keys AddModifiers(Keys key)
{
//CapsLock
if ((GetKeyState(VK_CAPITAL) & 0x0001) != 0) key = key | Keys.CapsLock;
//Shift
if ((GetKeyState(VK_SHIFT) & 0x8000) != 0) key = key | Keys.Shift;
//Ctrl
if ((GetKeyState(VK_CONTROL) & 0x8000) != 0) key = key | Keys.Control;
//Alt
if ((GetKeyState(VK_MENU) & 0x8000) != 0) key = key | Keys.Alt;
return key;
}
#endregion
Form1 kod:
private void Form1_Load(object sender, EventArgs e)
{
globalKeyboardHook gkh = new globalKeyboardHook();
gkh.KeyDown += new KeyEventHandler(gkh_KeyDown);
gkh.HookedKeys.Add(Keys.Space);
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
SearchValueMemory();
}
private void gkh_KeyDown(object sender, KeyEventArgs e)
{
if (tabControl1.Controls[0] == tabControl1.SelectedTab)
{
if (e.KeyCode == Keys.Space)
{
Debug.WriteLine("space pressed");
}
}
}
void SearchValueMemory()
{
Process process = new Process();
try
{
process = Process.GetProcessesByName("MyProcess")[0];
IntPtr pointerAdd = new IntPtr(0x0046A3DC);
api.ReadProcess = process;
api.OpenProcess();
ProcessModule processModule = null;
foreach (ProcessModule item in process.Modules)
{
if (item.FileName.Contains("MyProcess.exe"))
{
processModule = item;
}
}
pointerMemoryAddr = api.ReadPointerAddress(pointerAdd, 0x184, 4, out bytesout, true, (int)processModule.BaseAddress);
try
{
if (pointerMemoryAddr != -1)
{
int bytesRead;
byte[] memo = api.ReadMemory((IntPtr)pointerMemoryAddr, 4, out bytesRead);
Debug.WriteLine(memo[0]);
}
}
catch (Exception ex) {}
}
catch(Exception) {}
}