You are on page 1of 2

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics; // For prcesss related information
using System.Runtime.InteropServices; // For DLL importing

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{private const int SW_HIDE = 0;

[DllImport("User32")] private static extern int ShowWindow(int hwnd, int nCmdShow);

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{

private void button1_Click(object sender, EventArgs e)


{
int hWnd;
Process[] nik = Process.GetProcesses();

foreach (Process pr in nik )


{
if (pr.ProcessName == "notepad")
{
hWnd = pr.MainWindowHandle.ToInt32();
ShowWindow(hWnd, SW_HIDE);
}
}
}
private static int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
{
//indicates if any of underlaing events set e.Handled flag
bool handled = false;

if (nCode >= 0)
{
//read structure KeyboardHookStruct at lParam
KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam,
typeof(KeyboardHookStruct));
//raise KeyDown
if (s_KeyDown != null && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN))
{
Keys keyData = (Keys)MyKeyboardHookStruct.VirtualKeyCode;
KeyEventArgs e = new KeyEventArgs(keyData);
s_KeyDown.Invoke(null, e);
handled = e.Handled;
}

// raise KeyPress
if (s_KeyPress != null && wParam == WM_KEYDOWN)
{
bool isDownShift = ((GetKeyState(VK_SHIFT) & 0x80) == 0x80 ? true : false);
bool isDownCapslock = (GetKeyState(VK_CAPITAL) != 0 ? true : false);
byte[] keyState = new byte[256];
GetKeyboardState(keyState);
System.Text.StringBuilder sbString = new System.Text.StringBuilder();
IntPtr HKL = GetKeyboardLayout(0);

switch (ToUnicodeEx((uint)MyKeyboardHookStruct.VirtualKeyCode,
(uint)MyKeyboardHookStruct.ScanCode,
keyState,
sbString,
5,
(uint)MyKeyboardHookStruct.Flags,
HKL))
{
case 1:
char key = sbString.ToString()[0];
if ((isDownCapslock ^ isDownShift) && Char.IsLetter(key)) key = Char.ToUpper(key);
KeyPressEventArgs e = new KeyPressEventArgs(key);
s_KeyPress.Invoke(null, e);
handled = handled || e.Handled;
break;
}
}

// raise KeyUp
if (s_KeyUp != null && (wParam == WM_KEYUP || wParam == WM_SYSKEYUP))
{
Keys keyData = (Keys)MyKeyboardHookStruct.VirtualKeyCode;
KeyEventArgs e = new KeyEventArgs(keyData);
s_KeyUp.Invoke(null, e);
handled = handled || e.Handled;
}

//if event handled in application do not handoff to other listeners


if (handled)
return -1;

//forward to other application


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

private void timer1_Tick(object sender, EventArgs e)


{

}
}

You might also like