No announcement yet.

Deadly Code?

  • Filter
  • Time
  • Show
Clear All
new posts

  • Deadly Code?

    This is really odd. If I comment out the indicated lines, my keyboard will not work correctly - pressing keys gives the wrong result.

    I have to power down restart the machine for the keyboard to work again. I tried on a second keyboard with the same result.

    Try it at your own risk and if you do, let me know if you have the same result.
    'Compilable Example:  (Jose Includes)
    #Compiler PBWin 10
    #Compile Exe
    #Dim All
    #Include ""
    Global hDlg As Dword, hRichEdit As Dword
    %ID_RichEdit = 500
    Function PBMain() As Long
       Local style&, buf$
       buf$ =  "This is sample" + $CrLf + "text for the" + $CrLf + "edit control."
       style& = %WS_Child Or %WS_Visible Or %ES_MultiLine Or %WS_VScroll Or %ES_AutoHScroll _
          Or %WS_HScroll Or %ES_AutoVScroll Or %ES_WantReturn Or %ES_NoHideSel Or %WS_TabStop
       Dialog New Pixels, 0, "Test Code",300,300,200,150, %WS_OverlappedWindow To hDlg
       Control Add Button, hDlg, 100,"Push", 30,10,140,20
       LoadLibrary("riched32.dll") : InitCommonControls
       Control Add "RichEdit", hDlg, %ID_RichEdit, buf$,20,40,160,100, style&, %WS_Ex_ClientEdge
       Control Handle hDlg, %ID_RichEdit To hRichEdit
       Dialog Show Modal hDlg Call DlgProc
    End Function
    CallBack Function DlgProc() As Long
       If Cb.Msg = %WM_Command And Cb.Ctl = 100 And Cb.CtlMsg = %BN_Clicked Then
          Control Set Focus hDlg, %ID_RichEdit
          keybd_event(%VK_CONTROL, 0, 0, 0)
          keybd_event(%VK_A, 0, 0, 0)                       'remove these and keyboard fails
          keybd_event(%VK_A, 0, %KEYEVENTF_KEYUP, 0)        'remove these and keyboard fails
          keybd_event(%VK_CONTROL, 0, %KEYEVENTF_KEYUP, 0)  'remove these and keyboard fails
       End If
    End Function

  • #2

    Note This function has been superseded. Use SendInput instead.

    That said - without a KEYUP, the keyboard driver thinks that a key is being held down for ever


    • #3
      Howdy, Stuart!

      Yep, but it still works, at least in dozens of apps where I use it. None of my other apps give the keyboard problem.

      I just happened to try out the combination above and discovered the keyboard problem. I thought my keyboard died but then a second keyboard had the same problem.

      Did you happen to try it and see what happens?

      ... added ... in the past I looked at SendInput and it seemed unnecessarily complicated to use. I guess I need to look for a simpler introductory example. But regardless, I've never heard of keybd_event having any such problem.


      • #4
        Howdy, Stuart!

        You may be right about the missing keyup but I've tried other keybd_events (not Control) and the missing keyup did not cause a problem. I'll make an example and post it.

        It's surprising that the keyboard driver won't at least time out, or something, to prevent the keyboard from working as expected.


        • #5
          the code behaves as expected here.
          With the lines removed, there is no release of the ctrl key so any subsequent key press results in the equivalent ctrl character.

          If I press the A key, I get ctrl-A and that correctly selects all the text in the box.
          If I press the C key, I get ctrl-C and that correctly copies the selected text to the clipboard.

          To get out of that state the program needs to see a ctrl key released so try pressing the ctrl key again on the keyboard, which will give a ctrl press and a ctrl release and that release will then allow non-control characters to be typed.

          No need to restart the computer, just press the ctrl key.


          • #6
            Howdy, Paul and Stuart!

            Well that is all very straight forward. I was expecting something more "technical" to describe what I saw.