keyboard strokes

Dineshkumar.S 446 Reputation points
2022-09-09T10:26:14.757+00:00

I am developing an app using C # where i want to read the key strokes continously like two or more Ex Windows + M after that Insert + T so i have hook procedure for that and If i integerate it in my code and I am unable to read the key strokes continously I will give the code for the reference can anyone suggest me some ideads to clear this it will very helpful. TIA
if ((nCode >= 0))
{
KBDLLHOOKSTRUCT pKBDLLHOOKSTRUCT = new KBDLLHOOKSTRUCT();
pKBDLLHOOKSTRUCT = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, pKBDLLHOOKSTRUCT.GetType());

            if (wParam == (IntPtr)WM_KEYDOWN)  
            {  
                if (pKBDLLHOOKSTRUCT.vkCode == (short)Keys.M)  
                {  
                    bool bKeyDown = (GetAsyncKeyState((int)Keys.LWin) & 0x8000) == 0x8000;  
                    if (bKeyDown)  
                    {  
                          
                        return 1;  
                    }  
                }  
                if (pKBDLLHOOKSTRUCT.vkCode == (short)Keys.Tab)  
                {  
                    bool bKeyDown = (GetAsyncKeyState((int)Keys.Tab) & 0x8000) == 0x8000;  
                    if (bKeyDown)  
                    {  
                          
                        return 1;  
                    }   

after that It will read only 1st key combination and it is not reading the 2nd key combination and gettin error at mentioned below:-
attForm.ShowDialog(this); // error as mentioned below

Managed Debugging Assistant 'CallbackOnCollectedDelegate'
Message=Managed Debugging Assistant 'CallbackOnCollectedDelegate' : 'A callback was made on a garbage collected delegate of type 'EYEModelLib!EYE.Model.Utility_Classes.KeyBoardInterface+HookProc::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called.'

.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
323 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,320 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Castorix31 81,831 Reputation points
    2022-09-09T11:05:09.74+00:00

    Hook procedure and handle must be static
    And the second test has no sense (twice Keys.Tab...)

    1 person found this answer helpful.

  2. Dineshkumar.S 446 Reputation points
    2022-09-09T12:39:02.713+00:00

    if (wParam == (IntPtr)WM_KEYDOWN)
    {
    if (pKBDLLHOOKSTRUCT.vkCode == (short)Keys.M)
    {
    bool bKeyDown = (GetAsyncKeyState((int)Keys.LWin) & 0x8000) == 0x8000;
    if (bKeyDown)
    {

                         return 1;  
                     }  
                 }  
                 if (pKBDLLHOOKSTRUCT.vkCode == (short)Keys.Tab)  
                 {  
                     bool bKeyDown = (GetAsyncKeyState((int)Keys.Insert) & 0x8000) == 0x8000;  
                     if (bKeyDown)  
                     {  
                              
                         return 1;  
                     }   
    

    In this case also it is not reading the key strokes


  3. Jack J Jun 24,296 Reputation points Microsoft Vendor
    2022-10-05T09:32:28.437+00:00

    @Dineshkumar.S , you could try the following code to read the key strokes continously by setting a bool variable.

    Here is a code example you could refer to.

     private bool t = false;  
        private void Form1_KeyDown(object sender, KeyEventArgs e)  
                {  
                   if(e.KeyData== (Keys.Control|Keys.F))  
                    {  
                        t = true;  
                    }  
                   if(e.KeyData==(Keys.Alt|Keys.M)&&t)  
                    {  
                        MessageBox.Show("Test");  
                    }  
                }  
    

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.