Bonjour a tous,

j'essay de me familiariser avec les hook, j'essay de faire un hook clavier mais rien ne se passe !

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System.Reflection;
using System.Text;
using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Windows.Forms;
 
namespace Hook
{
    class HookClavier
    {
        #region Import Dll
        [DllImport("user32.dll")]
        static extern IntPtr GetForegroundWindow();
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool UnhookWindowsHookEx(IntPtr hhk);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
        IntPtr wParam, IntPtr lParam);
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr GetModuleHandle(string lpModuleName);
        #endregion
        #region Variables
        private const int WH_KEYBOARD_LL = 13;
        private const int WM_KEYDOWN = 0x0100;
        private LowLevelKeyboardProc _proc = HookCallback;
        private static IntPtr _hookID = IntPtr.Zero;
        #endregion
        #region Delegates
        private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
        #endregion
        public String CurrentWindowsTitle()
        {
            IntPtr p = GetForegroundWindow();
            if (p == IntPtr.Zero)
                return (String.Empty);
            StringBuilder d = new StringBuilder(256);
            GetWindowText(p, d, 256);
            return (Convert.ToString(d));
        }
        public static void Write()
        {
             try
            {
                    String Filename = "test.txt";
                    StreamWriter sw = new StreamWriter(System.IO.Path.Combine("C:\\", FileName), true, Encoding.Unicode);
                    sw.WriteLine("test");
                    sw.Close();
                }
            }
            catch (Exception)
            {
 
            }
        }
 
        private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
            {
                int vkCode = Marshal.ReadInt32(lParam);
                Keys key = (Keys)vkCode;
                HookClavier.Write();
            }
            return CallNextHookEx(_hookID, nCode, wParam, lParam);
        }
 
        public void Start()
        {
            if (_hookID == IntPtr.Zero)
            {
                using (Process curProcess = Process.GetCurrentProcess())
                using (ProcessModule curModule = curProcess.MainModule)
                {
                    _hookID = SetWindowsHookEx(WH_KEYBOARD_LL, _proc, GetModuleHandle(curModule.ModuleName), 0);
                }
            }
        }
        public void Stop()
        {
            if (_hookID != IntPtr.Zero)
                UnhookWindowsHookEx(_hookID);
        }
        public Boolean IsActivated
        {
            get
            {
                if (_hookID != IntPtr.Zero)
                    return (true);
                return (false);
            }
        }
    }
}
Mais rien ne sa passe je ne comprend pas pourquoi !
Si quelqu'un pouvait m'aiguiller ca serait chouette.

Merci.

Cordialement,
NeoKript