using System.Diagnostics;
using System.Runtime.InteropServices;
daha sonra kullanacağımız dll ve structure kaynak kodlarımızı ekleyelim.
[StructLayout(LayoutKind.Sequential)]
private struct KeyboardDLLStruct
{
public Keys key;
public int scanCode;
public int flags;
public int time;
public IntPtr extra;
}
private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int id, LowLevelKeyboardProc callback, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool UnhookWindowsHookEx(IntPtr hook);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hook, int nCode, IntPtr wp, IntPtr lp);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string name);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern short GetAsyncKeyState(Keys key);
Programımızda kullanacağımız Global değişkenleri tanımlayalım
private IntPtr ptrHook;
private LowLevelKeyboardProc objKeyboardProcess;
public Form1()
{
ProcessModule objCurrentModule = Process.GetCurrentProcess().MainModule;
objKeyboardProcess = new LowLevelKeyboardProc(captureKey);
ptrHook = SetWindowsHookEx(13, objKeyboardProcess, GetModuleHandle(objCurrentModule.ModuleName), 0);
InitializeComponent();
}
captureKey ile engellemek istediğimiz tuşları yazalım.
private IntPtr captureKey(int nCode, IntPtr wp, IntPtr lp)
{
if (nCode >= 0)
{
KeyboardDLLStruct objKeyInfo = (KeyboardDLLStruct)Marshal.PtrToStructure(lp, typeof(KeyboardDLLStruct));
if (objKeyInfo.key == Keys.RWin || objKeyInfo.key == Keys.LWin || objKeyInfo.key == Keys.Enter || objKeyInfo.key == Keys.F4)
{
return (IntPtr)1;
}
}
return CallNextHookEx(ptrHook, nCode, wp, lp);
}
Çalıştırdığınızda tusları engellediğinizi görebilirsiniz programınızı simge durumuna küçültseniz bile bu tuslar , program çalıştığı surece çalışmaz. Bazı durumlarda çalışıp bazı durumlarda çalışmamaısnı isterseniz bir bool değişkeni oluşturup if içerisine katabilirsiniz. bu değişken true olduğunda tusları aktif et , false olduğunda kapat gibi..
if ((objKeyInfo.key == Keys.RWin || objKeyInfo.key == Keys.LWin || objKeyInfo.key == Keys.Enter || objKeyInfo.key == Keys.F4) && isdurum == false)
Hiç yorum yok:
Yorum Gönder