PlanetSquires Forums

Support Forums => Other Software and Code => Topic started by: jthompson on June 20, 2007, 11:20:23 PM

Title: Capture keystrokes on a form with a progressbar and label.
Post by: jthompson on June 20, 2007, 11:20:23 PM
I've got a form intended to capture data from a magstripe reader.  The data comes in via the keyboard.  The data is not pretty to look at, and I was given the idea (on PB forums) to create a form with a progress bar that displays the progress as the magstripe is read (XX out of XX characters).  I made a small sample in PB, but upon integrating into FireFly it doesn't seem to capture any of the characters / keystrokes.  Does FF do something extra behind the scene I need to account for?

Thanks!

-John


'------------------------------------------------------------------------------------------------------------------------
Function FORM1_CUSTOM ( _
                      hWndForm      As Dword, _  ' handle of Form
                      wMsg          As Long,  _  ' type of message
                      wParam        As Dword, _  ' first message parameter
                      lParam        As Long   _  ' second message parameter
                      ) As Long
Static sString As String
Select Case wMsg
Case %WM_CHAR
Beep
'If Bit(lParam,31)=1 Then
sString = sString & Chr$(wParam)
FF_Control_SetText (HWND_FORM1_LABEL1, sString)
'End If
End Select
End Function
Title: Re: Capture keystrokes on a form with a progressbar and label.
Post by: TechSupport on June 21, 2007, 10:06:50 AM
Hi John,

A "Form" does not normally receive keyboard input. That is why you can not capture the WM_CHAR message. The easy solution is to create a TextBox and set its WindowStyle WS_VISIBLE to False. Then ensure that the TextBox gets Focus so it can recieve keyboard input. You can then easily trap the WM_CHAR message.

The example below is based on a TextBox called TXTINPUT and whenever a character is typed, the character is displayed in a Label.

Function FORM1_TXTINPUT_WM_CHAR ( _
                                ControlIndex  As Long,  _  ' index in Control Array
                                hWndForm      As Dword, _  ' handle of Form
                                hWndControl   As Dword, _  ' handle of Control
                                chCharCode    As Long,  _  ' character code
                                lKeyData      As Long   _  ' key data
                                ) As Long

   
   ' Display the incoming character in the Label
   FF_Control_SetText HWND_FORM1_LABEL1, Chr$(chCharCode)                       
   
   ' Clear the textbox
   FF_Control_SetText HWND_FORM1_TXTINPUT, ""
   
   ' Ensure that the focus remains on the textbox
   SetFocus HWND_FORM1_TXTINPUT   
   
End Function


Title: Re: Capture keystrokes on a form with a progressbar and label.
Post by: jthompson on June 22, 2007, 02:22:18 AM
Thanks Paul.  That is a great idea and works very well.  I've attached the full source code of what I was using (before changing to your suggestion).  It is from sweetheartgames.com demo SDK code with the WM_CHAR message added in.  But, they already have a textbox on their page (which I didn't know was a factor).

Thanks again!

-John


'JPRO_COMPILER = PB/WIN
#COMPILE EXE
#INCLUDE "WIN32API.INC"

GLOBAL ghInstance AS DWORD   

FUNCTION WINMAIN ( BYVAL hInstance AS LONG, BYVAL hPrevInstance AS LONG, BYVAL pszCmdLine AS ASCIIZ PTR, BYVAL nCmdShow AS LONG ) AS LONG

    LOCAL msg  AS tagMsg                 
    LOCAL hWnd  AS DWORD
    LOCAL szClassName   AS ASCIIZ * %MAX_PATH   
    LOCAL My_wclass     AS WNDCLASSEX         
   
    szClassName            = "MY_SDK_Example"
    My_wclass.cbSize        = SIZEOF(My_wclass)
    My_wclass.STYLE         = %CS_DBLCLKS     
    My_wclass.lpfnWndProc   = CODEPTR(My_WndProc)
    My_wclass.cbClsExtra    = 0   
    My_wclass.cbWndExtra    = 0 
    My_wclass.hInstance     = hInstance                                
    My_wclass.hIcon         = LOADICON(%NULL, BYVAL %IDI_APPLICATION)   
    My_wclass.hCursor       = LOADCURSOR(%NULL, BYVAL %IDC_ARROW)       
    My_wclass.hbrBackground = %COLOR_BTNFACE + 1                     
    My_wclass.lpszMenuName  = %NULL                             
    My_wclass.lpszClassName = VARPTR(szClassName)                   
    My_wclass.hIconSm       = LOADICON(%NULL, BYVAL %IDI_APPLICATION)   
    IF ISFALSE REGISTERCLASSEX(My_wclass) THEN
        FUNCTION = %FALSE
        EXIT FUNCTION
    END IF
   
    ghInstance = hInstance
   
    hWnd = CREATEWINDOWEX(%WS_EX_WINDOWEDGE, "MY_SDK_Example", "A BASIC WINDOW - SDK style", %WS_OVERLAPPEDWINDOW _
    ,200,200,380,200, %NULL, %NULL, hInstance, BYVAL %NULL)                                      ' handle of instance, creation parameters
   
    IF ISFALSE hWnd THEN
        FUNCTION = %FALSE
        EXIT FUNCTION
    END IF
   
    SHOWWINDOW hWnd, %SW_SHOW
    UPDATEWINDOW hWnd
   
    WHILE GETMESSAGE(Msg, %NULL, 0, 0) '----------- start message loop ----------
        TRANSLATEMESSAGE Msg
        DISPATCHMESSAGE Msg
    WEND '-----------  end message loop  ----------
   
    FUNCTION = %TRUE

END FUNCTION

'-------------------------------------------------------------------------------
FUNCTION My_WndProc (BYVAL hWnd AS LONG, BYVAL wMsg AS LONG, _
                  BYVAL wParam AS LONG, BYVAL lParam AS LONG) AS LONG
                 
  LOCAL hWndChild   AS DWORD 
  LOCAL hFont       AS DWORD   

  SELECT CASE wMsg
     
    CASE %WM_CREATE
      hFont = GETSTOCKOBJECT(%DEFAULT_GUI_FONT)
      hWndChild = CREATEWINDOWEX(%NULL,"Button","1st Button", %WS_CHILD OR %WS_VISIBLE , _
282,16,75,20, hWnd, 101, ghInstance, BYVAL %NULL)
      SENDMESSAGE hWndChild, %WM_SETFONT, hFont, %TRUE               

      hWndChild = CREATEWINDOWEX(%NULL,"Button","2nd Button", %WS_CHILD OR %WS_VISIBLE , _
282,43,75,20, hWnd, 102, ghInstance, BYVAL %NULL)
      SENDMESSAGE hWndChild, %WM_SETFONT, hFont, %TRUE               

      hWndChild = CREATEWINDOWEX(%NULL,"Button","Change Label", %WS_CHILD OR %WS_VISIBLE , _
282,70,75,20, hWnd, 103, ghInstance, BYVAL %NULL)
      SENDMESSAGE hWndChild, %WM_SETFONT, hFont, %TRUE               

      hWndChild = CREATEWINDOWEX(%NULL,"Button","Close Me", %WS_CHILD OR %WS_VISIBLE , _
282,140,75,20, hWnd, 104, ghInstance, BYVAL %NULL)
      SENDMESSAGE hWndChild, %WM_SETFONT, hFont, %TRUE               

      hWndChild = CREATEWINDOWEX(%WS_EX_STATICEDGE,"Static","This is a label", %WS_CHILD OR %WS_VISIBLE, _
14,16,250,17, hWnd, 105, ghInstance, BYVAL %NULL)
      SENDMESSAGE hWndChild, %WM_SETFONT, hFont, %TRUE               

      hWndChild = CREATEWINDOWEX(%WS_EX_CLIENTEDGE,"Edit","And add some text", %WS_CHILD OR %WS_VISIBLE OR _
%ES_MULTILINE OR %ES_WANTRETURN , 14,42,250,116, hWnd, 106, ghInstance, BYVAL %NULL)
      SENDMESSAGE hWndChild, %WM_SETFONT, hFont, %TRUE               

      FUNCTION = %FALSE
      EXIT FUNCTION
     
    CASE %WM_COMMAND
      SELECT CASE LOWRD(wParam)
        CASE 101
          IF HIWRD(wParam) = %BN_CLICKED THEN
            MSGBOX "you clicked me, I'm Button1"
          END IF

        CASE 102
          IF HIWRD(wParam) = %BN_CLICKED THEN
             DIM Txt AS ASCIIZ * %MAX_PATH
             GETDLGITEMTEXT hWnd,106,txt, SIZEOF(txt)
            MSGBOX "the text in Control ID# 106 is:" + $CRLF + $CRLF + txt$
          END IF

        CASE 103
          IF HIWRD(wParam) = %BN_CLICKED THEN
             SETDLGITEMTEXT hWnd,105," You Changed Me!!"
          END IF

        CASE 104
          IF HIWRD(wParam) = %BN_CLICKED THEN
            SENDMESSAGE hWnd, %WM_CLOSE, 0, 0
          END IF
      END SELECT

    CASE %WM_CHAR
        DIM sText AS STATIC STRING
        sText = sText & CHR$(wParam)
        SETDLGITEMTEXT hWnd, 106, BYCOPY sText
        'IF BIT(lParam,31) = 0 THEN OPEN "TXT.TXT" FOR APPEND AS #1 : PRINT #1, HEX$(wParam), CHR$(wParam) : CLOSE #1

    CASE %WM_DESTROY
      POSTQUITMESSAGE 0
      FUNCTION = %FALSE
      EXIT FUNCTION

  END SELECT
  FUNCTION = DEFWINDOWPROC(hWnd, wMsg, wParam, lParam)
END FUNCTION