Announcement

Collapse
No announcement yet.

How to catch Control Key + Other Key combinations?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to catch Control Key + Other Key combinations?

    How would you catch something like CTRL S where two keys are pressed at the same time?

    KeyboardHook and GetAsyncKeyState look like they only tell what one key is pressed/

  • #2
    David,


    look for ACCEL ATTACH in the help file


    JK

    Comment


    • #3
      GetAsyncKeyState once with control key virtual key code
      GetAsyncKeyState a second time for s key.

      I am assuming this goes with the get keys even if window does not have focus thread???

      Otherwise (has focus) I would use %WM_KEYDOWN and %WM_KEYUP, and 3 static variables in callback to track state of SHIFT, CONTROL and ALT keys.

      Cheers,
      Dale

      Comment


      • #4
        If it's a "no focus - any program" example you need, try:
        Code:
        #COMPILE EXE
        #DIM ALL
        #INCLUDE "WIN32API.INC"
        
        '====================================================================
        FUNCTION PBMAIN()
          LOCAL hDlg AS LONG
          DIALOG NEW 0 ,"KeyBoard trap - press Ctrl + Letter",,, 200, 100, _
                     %WS_CAPTION OR %WS_SYSMENU, 0 TO hDlg
          DIALOG SHOW MODAL hDlg CALL DlgProc
        END FUNCTION
        
        '====================================================================
        CALLBACK FUNCTION DlgProc
          LOCAL c AS LONG, sBuf AS STRING
          STATIC hTimer AS DWORD
        
          SELECT CASE CB.MSG
          CASE %WM_INITDIALOG
              SetWindowPos(CB.HNDL, %HWND_TOPMOST, 0, 0, 0, 0, %SWP_NOSIZE OR %SWP_NOMOVE)
              hTimer = SetTimer(CB.HNDL, 1, 10, 0) ' start timer
        
          CASE %WM_TIMER
              IF GetAsyncKeyState(%VK_CONTROL) THEN
                  sBuf = "%VK_CONTROL"
                  FOR c = 65 TO 90  ' A - Z
                      IF GetAsyncKeyState(c) THEN
                          sBuf = sBuf + " + " + CHR$(c)
                          EXIT FOR
                      END IF
                  NEXT
              ELSE
                 sBuf = "KeyBoard trap - press Ctrl + Letter"
              END IF
              DIALOG SET TEXT CB.HNDL, sBuf
        
          CASE %WM_Destroy
              IF hTimer THEN KillTimer CB.HNDL, hTimer ' kill timer
        
          END SELECT
        END FUNCTION

        Comment


        • #5
          I remember one of our powerbasic gurus postings some code before because I tested it. I caught single key presses for sure and it think the code was very short and it was a GUI program.
          I want to say it was Burns who posted it. But that was a few yeas ago and I cannot be sure
          p purvis

          Comment


          • #6
            Another way to catch all key's status in one take is via GetKeyboardState() and then look at returned array, something like:
            Code:
              LOCAL c AS LONG, sBuf AS STRING
              DIM lpKeyState(255) AS BYTE
              GetKeyboardState BYVAL VARPTR(lpKeyState(0))
              FOR c = 0 TO 255
                 sBuf = sBuf + STR$(c) + ":" + STR$(lpKeyState(c)) + "     "
                 IF c AND (c MOD 10 = 0) THEN sBuf = sBuf + $CRLF
              NEXT
              ? sBuf
            When a key is pressed, it's matching array item holds value 128 or 129 (pressed, toggled state) and some un-pressed keys vill have value 1 if they have been set (for example Caps Lock (array item 20), etc..

            Comment


            • #7
              Am I missing something here?

              What is wrong with checking that

              LEN(input_key) = 1 and ASC(MID$(input_key,1,1) = 19
              [I]I made a coding error once - but fortunately I fixed it before anyone noticed[/I]
              Kerry Farmer

              Comment


              • #8
                Think i
                posted in the wrong thread on error
                My thoughts were on all keys
                p purvis

                Comment


                • #9
                  The code I was referring to came from Wayne Diamond and i never used the code in a program.
                  I just tried it and his loop did not spin up my CPU.
                  Other code seems to be there like Borge's
                  https://forum.powerbasic.com/forum/u...-windows/60054
                  But when Borje talks, i listen.
                  p purvis

                  Comment

                  Working...
                  X