Autor Beitrag
michse02
Hält's aus hier
Beiträge: 1



BeitragVerfasst: Fr 21.03.08 18:21 
Hallo,

folgendes Problem:

Ich möchte die F Tasten in meinem Programm abfragen. Und zwar auch, wenn mein Programm keinen Focus hat (weils in den Systray minimiert ist)

Dazu hab ich folgendes Implementiert:

ausblenden volle Höhe C#-Quelltext
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:
 [DllImport("user32.dll")]
        private static extern IntPtr SetWindowsHookEx(HookType code, HookProc func, IntPtr hInstance, int threadID);

        [DllImport("user32.dll")]
        private static extern int CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

        private delegate int HookProc(int code, IntPtr wParam, IntPtr lParam);
        private HookProc myCallbackDelegate = null;

        public enum HookType : int
        {
            WH_JOURNALRECORD = 0,
            WH_JOURNALPLAYBACK = 1,
            WH_KEYBOARD = 2,
            WH_GETMESSAGE = 3,
            WH_CALLWNDPROC = 4,
            WH_CBT = 5,
            WH_SYSMSGFILTER = 6,
            WH_MOUSE = 7,
            WH_HARDWARE = 8,
            WH_DEBUG = 9,
            WH_SHELL = 10,
            WH_FOREGROUNDIDLE = 11,
            WH_CALLWNDPROCRET = 12,
            WH_KEYBOARD_LL = 13,
            WH_MOUSE_LL = 14
        }


 public Form1()
        {
            InitializeComponent();
            
            // initialize our delegate
            this.myCallbackDelegate = new HookProc(this.KeyPressed);

            // setup a global gui-thread mouse hook
            SetWindowsHookEx(HookType.WH_KEYBOARD, this.myCallbackDelegate, IntPtr.Zero, AppDomain.GetCurrentThreadId());
        
        }



 private int KeyPressed(int code, IntPtr wParam, IntPtr lParam)
        {
            if (code < 0return CallNextHookEx(IntPtr.Zero, code, wParam, lParam);
            int i = wParam.ToInt32();
            if (i >= 112 && i <= 123)
            {
                //112 = f1, 113=f2, usw
               i -= 111;
                abspielen(i);
            }
            
            //return the value returned by CallNextHookEx
            return CallNextHookEx(IntPtr.Zero, code, wParam, lParam);
        }


Problem 1:
die abspielen Funktion wird immer 3 mal aufgerufen. Warum? Und was macht man dagegen?
Problem 2:
Das funktioniert nur, wenn man den Fokus hat. Wie erreiche ich, das es auch ohne den Fokus funktioniert? (Sprich das rogramm im Systray liegt)

Gruß

michse02

Edit: Hat sich erledigt...

Moderiert von user profile iconChristian S.: Code- durch C#-Tags ersetzt
Moderiert von user profile iconChristian S.: Topic aus C# - Die Sprache verschoben am Fr 21.03.2008 um 17:35