keysrokes reading

Dineshkumar.S 456 Reputation points
2022-08-18T12:03:21.56+00:00

How to read the keystrokes using c# and for example I need to verify if a user press the windows + D to go desktop my tool need to verify that the user has pressed those key combinations ?? can you suggest me ideas ? TIA

Developer technologies | .NET | .NET CLI
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 90,686 Reputation points
    2022-08-18T12:54:16.96+00:00

    For example, with a Keyboard Hook =>

           // Variables  
            static int hHook = 0;  
            HookProc KeyboardLLProcedure;          
              
            // Code  
            KeyboardLLProcedure = new HookProc(KeyboardLLProc);     
            hHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardLLProcedure, (IntPtr)0, 0);  
              
            // Procedure  
            public static int KeyboardLLProc(int nCode, IntPtr wParam, IntPtr lParam)  
            {  
                if ((nCode >= 0))  
                {  
                    KBDLLHOOKSTRUCT pKBDLLHOOKSTRUCT = new KBDLLHOOKSTRUCT();  
                    pKBDLLHOOKSTRUCT = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, pKBDLLHOOKSTRUCT.GetType());  
      
                    if (wParam == (IntPtr)WM_KEYDOWN)  
                    {  
                        if (pKBDLLHOOKSTRUCT.vkCode == (short)Keys.D)  
                        {  
                            bool bKeyDown = (GetAsyncKeyState((int)Keys.LWin) & 0x8000) == 0x8000;  
                            if (bKeyDown)  
                            {  
                                Console.Beep(8000, 10);  
                                Console.WriteLine("[Win] + D intercepted");  
                                return 1;  
                            }  
                        }  
                    }                 
                }  
                return nCode < 0 ? CallNextHookEx(hHook, nCode, wParam, lParam) : 0;  
            }  
              
            // Declarations  
            public const int WH_KEYBOARD_LL = 13;  
            public const int WM_KEYDOWN = 0x0100;  
              
            [DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]  
            public static extern short GetAsyncKeyState(int nVirtKey);  
              
            [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Unicode)]  
            public struct KBDLLHOOKSTRUCT  
            {            
                public int vkCode;  
                public int scanCode;  
                public int flags;  
                public int time;  
                public uint dwExtraInfo;  
            }        
      
            public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);  
      
            [DllImport("User32.dll", CharSet = CharSet.Auto,  CallingConvention = CallingConvention.StdCall)]  
            public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);  
      
            [DllImport("User32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]  
            public static extern bool UnhookWindowsHookEx(int idHook);  
      
            [DllImport("User32.dll", CharSet = CharSet.Auto,   CallingConvention = CallingConvention.StdCall)]  
            public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);  
    
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Dineshkumar.S 456 Reputation points
    2022-08-18T16:22:53.25+00:00

    do this will read any key stroke combinations for example like Insert + T , Insert + Up arrow can you clarify that ?? If you clarify me it will be very helpful TIA


  2. Dineshkumar.S 456 Reputation points
    2022-08-23T06:04:06.743+00:00

    Variables233889-kc-10.png


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.