Announcement

Collapse
No announcement yet.

capturing simulated keyboard input

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

  • capturing simulated keyboard input

    I am encountering a real headache and will be grateful for any suggestion. I am trying to capture simulated keystroke from a device, and I try to keep listening to any sequence coming in without suspending other functions, hence my desire to avoid using line or text inputting method. Followings are methods tried:

    1) using graphic inkey and without any sleep, it works pretty well except it is consuming CPU like mad. I use wait key with 1 millisecond timeout then switch to inkey in burst mode or just sleep 1 millisecond in the listen loop, both resulting in occasional lost character within a sequence.

    2) using call back event WM_KEYDOWN from a dialog but the wparam is virtual key and not
    character. I then attempt use winapi translatemessage but never get wm_char event generated. (For translate message I use msg_type to copy the variables from CB)

    Any wise suggestion ?

    Kunhock

  • #2
    First of all, keystrokes will not come to your program unless one of your program's windows has the keyboard focus.

    If you want to use the keydown (better keyUP I'll explain in a minuite), you can trap virtual keys by reading wParam (param 3 to your window procedure). Seems to me at this point you have received the keystroke and I don't understand why you would be trying to translateMessage().

    When you use WM_KEYDOWN, be advised if the user holds the key down you will get multiple WM_KEYDOWN notifications and your code has to handle that; however, you will only get ONE WM_KEYUP notification.

    Also re:
    and I try to keep listening to any sequence coming in without suspending other functions,
    MULTIPLE THREADS OF EXECUTION!

    And I'd forget about using GRAPHICS INKEY as I am pretty sure that was designed as a blocking function.

    I don't understand the whole design.. If I had a device and it could provide a keystroke notification, 'd probably see if I could set up some kind of queue.. On the "device side" of the queue the device just deposits the notrification in the queue... on the other side of the queue, you pick out notifications one by one as your program is ready to process them. That's why we use queues in the first place.

    In case you can't tell, I feel kind of in the dark about "what you are really doing here (as expressed in English)."

    MCM
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      HI Many thanks for your kind response. I guess I have not explained very well the situation I was in, and here it is:

      The application is a simple membership mgt system, and I intend it to be a single window design, so all keystroke will indeed go to this window. It is not really multitasking per say, just that I want the screen to be clickable and the member id card RFID reader, which functions as an additional keyboard style USB interface, to be able trigger automatic member details and prompt for action.

      You were right about me getting to the stage where it is possible to use dialog callback to sense and capture the card reader input, and yes, I should be using wm_keyup as advised. You have given me the confidence I was doing the right thing.

      Reason for my doubt ? I found lower case a, for instance and upper case A are both inputted as virtual keycode of 65.

      The card id is numeric, and I checked confirm they do matched to the ascii codes, so for my application, I am actually solved. I do however now got curious , assuming I do need the alphabets, how to tell between an upper A and a lower a ?

      Regards.

      Kunhock

      Comment


      • #4
        "The card id is numeric, and I checked confirm they do matched to the ascii codes"

        Oh sorry, I also have found out now that there are two set of numeric keys, one set from NUMPAD.

        Comment


        • #5
          Originally posted by kunhock Koo View Post
          the member id card RFID reader, which functions as an additional keyboard style USB interface, to be able trigger automatic member details and prompt for action.
          Sounds like it's a simple "keyboard wedge". Don't think of it as "virutal keystrokes". Think of it as someone entering the membership number on a second keyboard followed by the enter key.'
          Try setting focus to a textbox before activating the card reader and see if it receives the data.


          Comment


          • #6
            What you want is probably %WM_CHAR (a/A, etc) messages, but problem is DDT dialogs eats those messages for breakfast, and it doesn't even help with subclassing. In SDK windows no problem, but DDT.. Anyway - there are ways - one can peek at messages before DDT engine swallows %WM_CHAR, using modeless dialog and PeekMessage - se example below. I found this old test code that traps and even translates keypresses to human language. Another way could be by using GetKeyState(..) in some way, but the following should work pretty well to. At least something to play with.
            '
            Code:
            #COMPILE EXE
            #DIM ALL
            #INCLUDE "WIN32API.INC"
            
            '====================================================================
            FUNCTION PBMAIN() AS LONG
              LOCAL hDlg AS DWORD
              DIALOG NEW 0, "Press a key",,,200,100, %WS_CAPTION OR %WS_SYSMENU TO hDlg
              CONTROL ADD TEXTBOX, hDlg, 123, "", 5, 5, 190, 13
            
              DIALOG SHOW MODELESS hDlg CALL DlgProc
            
            '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
              LOCAL Msg AS tagMsg, zKey AS ASCIIZ * 64
              DO
                  DIALOG DOEVENTS
                  PeekMessage(Msg, 0, 0, 0, %PM_NOREMOVE)
                  SELECT CASE Msg.message
                  CASE %WM_CHAR  ' a / A, etc.
                      IF IsCharAlphaNumeric(Msg.wParam) THEN
                          zKey = CHR$(Msg.wParam)
                      ELSE
                          GetKeyNameText(Msg.wParam, zKey, SIZEOF(zKey))
                      END IF
                      DIALOG SET TEXT hDlg, "%WM_CHAR   " + zKey + "    ASC(" + FORMAT$(Msg.wParam) + ")"
            
                  CASE %WM_KEYDOWN
                      IF IsCharAlphaNumeric(Msg.lParam) THEN
                          zKey = CHR$(Msg.lParam)
                      ELSE
                          GetKeyNameText(Msg.lParam, zKey, SIZEOF(zKey))
                      END IF
                      DIALOG SET TEXT hDlg, "%WM_KEYDOWN   " + zKey + "    ASC(" + FORMAT$(Msg.wParam) + ")"
                  END SELECT
              LOOP WHILE ISWIN(hDlg)
            
            
            END FUNCTION
            
            '====================================================================
            CALLBACK FUNCTION DlgProc() AS LONG
              SELECT CASE CB.MSG
              CASE %WM_INITDIALOG
            
              CASE %WM_COMMAND
                  SELECT CASE AS LONG CB.CTL
                  CASE %IDCANCEL
                      IF CB.CTLMSG = %BN_CLICKED OR CB.CTLMSG = 1 THEN 'end prog
                          DIALOG END CB.HNDL
                      END IF
                  END SELECT
            
              CASE %WM_DESTROY
            
              END SELECT
            END FUNCTION
            '
            Last edited by Borje Hagsten; 17 Mar 2021, 03:51 AM.

            Comment


            • #7
              Originally posted by kunhock Koo View Post
              1) using graphic inkey and without any sleep, it works pretty well except it is consuming CPU like mad. I use wait key with 1 millisecond timeout then switch to inkey in burst mode or just sleep 1 millisecond in the listen loop, both resulting in occasional lost character within a sequence.
              If you set up a timer to check graphic inkey, rather than employing a loop, it will not consume CPU cycles.
              The world is strange and wonderful.*
              I reserve the right to be horrifically wrong.
              Please maintain a safe following distance.
              *wonderful sold separately.

              Comment


              • #8

                The application is a simple membership mgt system, and I intend it to be a single window design, so all keystroke will indeed go to this window. It is not really multitasking per say, just that I want the screen to be clickable and the member id card RFID reader, which functions as an additional keyboard style USB interface, to be able trigger automatic member details and prompt for action.
                REALLY?

                You need to see this thread...

                https://forum.powerbasic.com/forum/c...dance-software

                .. and drop me an email . I think I'd be real open to working on this with you!

                Michael Mattias
                Port Washington WI
                [email protected]

                PS: I just checked your profile. I have no plans to visit Borneo any time soon so any cooperation will have to be of the remote variety!
                Michael Mattias
                Tal Systems (retired)
                Port Washington WI USA
                [email protected]
                http://www.talsystems.com

                Comment


                • #9
                  Hi Kurt,
                  "If you set up a timer to check graphic inkey, rather than employing a loop, it will not consume CPU cycles"

                  Yes I tried this down to 1 milli sec, which is the smallest it can do, it works pretty well but will lost some char coming in from "key wedge"

                  Comment


                  • #10
                    Borje,

                    yes thanks for the additional things to play with ...great gift ..

                    Comment


                    • #11
                      Hi Stuart,

                      "Sounds like it's a simple "keyboard wedge". Don't think of it as "virutal keystrokes". Think of it as someone entering the membership number on a second keyboard followed by the enter key.'
                      Try setting focus to a textbox before activating the card reader and see if it receives the data."

                      yes, I have to resort to using inbox at end of day, I was happy that I was able to read virtual key from dialog events, then when I add my planned graphic control to act as canvas ..it start eating my keystrokes .. and I attempt to focus to dialog rather then control .I cannot seem to do that using set focus, it need a control id, I tried what seem right, using %idd_dialog1, still to now prevail .

                      My initial hesitancy to using textbox is I need to time between the start char and end char so I can be sure these are from key wedge and not manually typed in , but now that I realised each key creates a wm_Command event, the problem is solved.

                      Comment


                      • #12
                        "Borje, yes thanks for the additional things to play with ...great gift ."

                        HI Borje,

                        gratitude ! your sample code exactly fits my requirement .. I realised now textbox cannot work well as the char 13 is never captured, hence I can never check if input has ended .

                        You doEvents method trap everything I wanted .. and yes ..many many thanks again !


                        Kunhock

                        Comment


                        • #13
                          Originally posted by kunhock Koo View Post
                          Hi Kurt,
                          "If you set up a timer to check graphic inkey, rather than employing a loop, it will not consume CPU cycles"

                          Yes I tried this down to 1 milli sec, which is the smallest it can do, it works pretty well but will lost some char coming in from "key wedge"
                          I see. Did you try using a loop when the timer finds a character, until there are none available?
                          The world is strange and wonderful.*
                          I reserve the right to be horrifically wrong.
                          Please maintain a safe following distance.
                          *wonderful sold separately.

                          Comment


                          • #14
                            Hi Kurt,

                            waitkey-> yes, still occasional key lost.

                            Comment

                            Working...
                            X