Page 1 of 5

Intercept the *Return Key*

Posted: Wed Feb 01, 2012 11:37 am
by Randy Walker
Why is this always an ever changing *workaround* problem?
Press the return key and go do something << thats all I'm asking for :(
Am I the only one that uses the enter key?
Or should I say, am I the only one that uses the enter key wrong?
Seems to me being able to detect the enter key, return key (whatever you want to call it) should be easy as falling off a rock.

I have 24,000+ lines of code and almost every time there is a version change and I upgrade, I find something changed in the way the return key is handled. Every time that happens I spend weeks trying to sort out where my code is broken and ultimately comes back to half dozen goofy code girations I've had to apply to try to detect use of the retun key.

Now, again, plain as day I see this in the v4.60 help and I think: :shock: FINALLY ... I FOUND DOCUMENTED SOLUTION!!!! :D

Code: Select all

  Repeat
    EventID = WaitWindowEvent()
    
    If EventID = #PB_Event_Gadget

      Select EventGadget()
        Case 0
          If EventType() = #PB_EventType_ReturnKey
            MessageRequester("Info", "Return key pressed", 0)
            SetActiveGadget(0)
          EndIf
^ ^ ^ it says right there -- If EventType() = #PB_EventType_ReturnKey ^ ^ ^

... But, even that does nothing. Nothing mentioned in the PBv4.60 Help/History about ''PB_EventType_ReturnKey''.

I think I have tried every possible *workaround* solution. Everything from AddKeyboardShortcut(), callbacks and Windows API. Somebody must know a cast iron rock solid cross platform proper method of detecting use of the return key -- that is not a workaround. Fred? Fr34k? Somebodyyyyyy? :cry:

Code snippet above came from PBv4.60 Help -- sample code: 'Gadget.pb'

Code: Select all

;
; ------------------------------------------------------------
;
;   PureBasic - Gadget example file
;
;    (c) 2002 - Fantaisie Software
;
; ------------------------------------------------------------
;

#WindowWidth  = 390
#WindowHeight = 350

If OpenWindow(0, 100, 200, #WindowWidth, #WindowHeight, "PureBasic - Gadget Demonstration", #PB_Window_MinimizeGadget)
    
  Top = 10
  GadgetHeight = 24

  Frame3DGadget(#PB_Any, 10, Top, 370, 290, "Player...") : Top+20

  StringGadget(0,  20, Top, 200, GadgetHeight, "")
  ButtonGadget(1, 223, Top,  72, GadgetHeight, "Play")
  ButtonGadget(2, 295, Top,  72, GadgetHeight, "Stop")  : Top+35
  DisableGadget(2,1)
  
  GadgetToolTip(1,"Play the current song")
  
  PanelGadget(3, 20, Top, #WindowWidth-50, #WindowHeight-Top-60)
    AddGadgetItem(3, 0, "MP3 PlayList")
      ListViewGadget(4, 6, 10, 230, 148)

      For k=0 To 30
        AddGadgetItem(4, -1, "Music Song n° "+Str(k))
      Next

      ButtonGadget(5,  250, 10, 80, GadgetHeight, "Add")
      ButtonGadget(6,  250, 38, 80, GadgetHeight, "Remove")
      ButtonGadget(7,  250, 66, 80, GadgetHeight, "Select")
      GadgetToolTip(7, "Select the current song")
      
      TrackBarGadget(17, 10, 168, 310, 25, 0, 100)

    AddGadgetItem(3, 1, "Options")
      Top = 10
      CheckBoxGadget(10, 10, Top, 250, GadgetHeight, "Enable low-pass filter") : Top+30
      CheckBoxGadget(11, 10, Top, 250, GadgetHeight, "Enable visual plug-in")  : Top+30
      ComboBoxGadget(12, 10, Top, 250, 21) : Top+30
        AddGadgetItem(12, -1, "FireWorks")
        AddGadgetItem(12, -1, "OpenGL spectrum")
        AddGadgetItem(12, -1, "Bump bass")
      SetGadgetState(12,0)
      DisableGadget(12,1)
      
      OptionGadget(13, 10, Top, 80, GadgetHeight, "640*480") : Top+20
      OptionGadget(14, 10, Top, 80, GadgetHeight, "800*600") : Top+20
      OptionGadget(15, 10, Top, 80, GadgetHeight, "1024*768")
      SetGadgetState(13, 1)
      
      ButtonGadget(16, 150, Top, 80, GadgetHeight, "Info")
  CloseGadgetList()

  TextGadget  (9, 10, #WindowHeight-30, 250, 24, "PureBasic - Gadget demonstration")
  ButtonGadget(8, #WindowWidth-100, #WindowHeight-36, 80, 24, "Quit")

  SetGadgetState(3, 0)

  Repeat
    EventID = WaitWindowEvent()
    
    If EventID = #PB_Event_Gadget

      Select EventGadget()
        Case 0
          If EventType() = #PB_EventType_ReturnKey
            MessageRequester("Info", "Return key pressed", 0)
            SetActiveGadget(0)
          EndIf
          
        Case 1 ; Play
          DisableGadget(2,0)  ; Enable the 'Stop' gadget
          DisableGadget(1,1)  ; Disable the 'Play' Gadget
      
        Case 2 ; Stop
          DisableGadget(1,0)  ; Enable the 'Play' gadget
          DisableGadget(2,1)  ; Disable the 'Stop' Gadget
        
        Case 4
          If EventType() = 2
            SetGadgetText(0, GetGadgetText(4)) ; Get the current item from the ListView..
          EndIf

        Case 5 ; Add
          AddGadgetItem(4, -1, "New Item Added...")

        Case 6 ; Remove
          RemoveGadgetItem(4, GetGadgetState(4)) ; Remove the current element of the ListView

        Case 7 ; Select
          SetGadgetText(0, GetGadgetText(4)) ; Get the current item from the ListView..
  
        Case 8 ; Quit...
          EventID = #PB_Event_CloseWindow

        Case 11 ; Enable PlugIn..
          DisableGadget(12, 1-GetGadgetState(11))
          
        Case 16 ;
          If GetGadgetState(13) : Result$ = GetGadgetText(13) : EndIf
          If GetGadgetState(14) : Result$ = GetGadgetText(14) : EndIf
          If GetGadgetState(15) : Result$ = GetGadgetText(15) : EndIf
         
          MessageRequester("Info", "Selected screen mode: "+Result$, 0)
        
        Case 17
          SetGadgetText(0, Str(GetGadgetState(17)))
          
      EndSelect

    EndIf

  Until EventID = #PB_Event_CloseWindow

EndIf

End  

Re: Intercept the *Return Key*

Posted: Wed Feb 01, 2012 12:24 pm
by Demivec
Randy Walker wrote:Now, again, plain as day I see this in the v4.60 help and I think: FINALLY ... I FOUND DOCUMENTED SOLUTION!!!!
That solution, using the #PB_EventType_ReturnKey, was added in version 3.10. It doesn't appear to be currently active and the constant isn't listed in the PureBasic constants. And from my testing it doesn't seem to have worked since version 4.31. That would put it at more than 2.5 years out-of-date as an effective solution.

I haven't sought to address your request for a rock-solid solution. I'll check back a little later.

Re: Intercept the *Return Key*

Posted: Wed Feb 01, 2012 12:46 pm
by RASHAD
Not the best but no API

Code: Select all


#WindowWidth  = 390
#WindowHeight = 350

If OpenWindow(0, 100, 200, #WindowWidth, #WindowHeight, "PureBasic - Gadget Demonstration",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
    
  Top = 10
  GadgetHeight = 24

  Frame3DGadget(#PB_Any, 10, Top, 370, 290, "Player...") : Top+20

  StringGadget(0,  20, Top, 200, GadgetHeight, "")
  ButtonGadget(1, 223, Top,  72, GadgetHeight, "Play")
  ButtonGadget(2, 295, Top,  72, GadgetHeight, "Stop")  : Top+35
  DisableGadget(2,1)
  
  GadgetToolTip(1,"Play the current song")
  
  PanelGadget(3, 20, Top, #WindowWidth-50, #WindowHeight-Top-60)
    AddGadgetItem(3, 0, "MP3 PlayList")
      ListViewGadget(4, 6, 10, 230, 148)

      For k=0 To 30
        AddGadgetItem(4, -1, "Music Song n° "+Str(k))
      Next

      ButtonGadget(5,  250, 10, 80, GadgetHeight, "Add")
      ButtonGadget(6,  250, 38, 80, GadgetHeight, "Remove")
      ButtonGadget(7,  250, 66, 80, GadgetHeight, "Select")
      GadgetToolTip(7, "Select the current song")
      
      TrackBarGadget(17, 10, 168, 310, 25, 0, 100)

    AddGadgetItem(3, 1, "Options")
      Top = 10
      CheckBoxGadget(10, 10, Top, 250, GadgetHeight, "Enable low-pass filter") : Top+30
      CheckBoxGadget(11, 10, Top, 250, GadgetHeight, "Enable visual plug-in")  : Top+30
      ComboBoxGadget(12, 10, Top, 250, 21) : Top+30
        AddGadgetItem(12, -1, "FireWorks")
        AddGadgetItem(12, -1, "OpenGL spectrum")
        AddGadgetItem(12, -1, "Bump bass")
      SetGadgetState(12,0)
      DisableGadget(12,1)
      
      OptionGadget(13, 10, Top, 80, GadgetHeight, "640*480") : Top+20
      OptionGadget(14, 10, Top, 80, GadgetHeight, "800*600") : Top+20
      OptionGadget(15, 10, Top, 80, GadgetHeight, "1024*768")
      SetGadgetState(13, 1)
      
      ButtonGadget(16, 150, Top, 80, GadgetHeight, "Info")
  CloseGadgetList()

  TextGadget  (9, 10, #WindowHeight-30, 250, 24, "PureBasic - Gadget demonstration")
  ButtonGadget(8, #WindowWidth-100, #WindowHeight-36, 80, 24, "Quit")

  SetGadgetState(3, 0)
  
  AddKeyboardShortcut(0, #PB_Shortcut_Return, 15)

  Repeat
    Select WaitWindowEvent()
    
     Case #PB_Event_Menu
; 
         Select EventMenu()
           Case 15
           If GetActiveGadget() = 0
             MessageRequester("Info", "Return key pressed", 0)
           EndIf
         EndSelect
         
    Case #PB_Event_CloseWindow
       Quit = 1
    
    Case #PB_Event_Gadget

      Select EventGadget()
        Case 0
          
        Case 1 ; Play
          DisableGadget(2,0)  ; Enable the 'Stop' gadget
          DisableGadget(1,1)  ; Disable the 'Play' Gadget
      
        Case 2 ; Stop
          DisableGadget(1,0)  ; Enable the 'Play' gadget
          DisableGadget(2,1)  ; Disable the 'Stop' Gadget
        
        Case 4
          If EventType() = 2
            SetGadgetText(0, GetGadgetText(4)) ; Get the current item from the ListView..
          EndIf

        Case 5 ; Add
          AddGadgetItem(4, -1, "New Item Added...")

        Case 6 ; Remove
          RemoveGadgetItem(4, GetGadgetState(4)) ; Remove the current element of the ListView

        Case 7 ; Select
          SetGadgetText(0, GetGadgetText(4)) ; Get the current item from the ListView..
  
        Case 8 ; Quit...
          Quit = 1

        Case 11 ; Enable PlugIn..
          DisableGadget(12, 1-GetGadgetState(11))
          
        Case 16 ;
          If GetGadgetState(13) : Result$ = GetGadgetText(13) : EndIf
          If GetGadgetState(14) : Result$ = GetGadgetText(14) : EndIf
          If GetGadgetState(15) : Result$ = GetGadgetText(15) : EndIf
         
          MessageRequester("Info", "Selected screen mode: "+Result$, 0)
        
        Case 17
          SetGadgetText(0, Str(GetGadgetState(17)))
          
      EndSelect

    EndSelect

  Until Quit = 1

EndIf

End  

Edit :Fixed the use of the close button

Re: Intercept the *Return Key*

Posted: Wed Feb 01, 2012 1:41 pm
by Randy Walker
RASHAD wrote:Not the best but no API
Thanks for offering the workaround, and you're right. Its not the best. Did you happen to notice in modifying the code, you also broke the #PB_Event_CloseWindow so that it no longer responds to the [x] close button in the far right corner of the title bar? :D
Whooopsie :oops:

Re: Intercept the *Return Key*

Posted: Wed Feb 01, 2012 1:45 pm
by MachineCode
AddKeyboardShortcut() is meant to be used with menus (even the manual says it), but people are using it as a general keyboard trapper and then losing functionality in other areas as a result.

Re: Intercept the *Return Key*

Posted: Wed Feb 01, 2012 1:47 pm
by RASHAD
You do not need the close button because you have Quit Button :mrgreen:

Change

Code: Select all

If OpenWindow(0, 100, 200, #WindowWidth, #WindowHeight, "PureBasic - Gadget Demonstration", #PB_Window_MinimizeGadget)

To

Code: Select all

If OpenWindow(0, 100, 200, #WindowWidth, #WindowHeight, "PureBasic - Gadget Demonstration",#PB_Window_ScreenCentered|0)

Code: Select all

           If GetActiveGadget() = 0
             MessageRequester("Info", "Return key pressed", 0)
           EndIf
It will be functioning only with the StringGadget(0)

Re: Intercept the *Return Key*

Posted: Wed Feb 01, 2012 1:56 pm
by Randy Walker
Demivec wrote:I haven't sought to address your request for a rock-solid solution. I'll check back a little later.
That would be such a welcome sight. I've employeed just about every conceivable workaroud for this, over and over again, layered on top of each other to the point I can't even read my code. It has so many supplimental patches in it trying to fix what the other workaround broke (like the workaround Rashad just posted, which would require a patch to fix the broken [x] close button).

Re: Intercept the *Return Key*

Posted: Wed Feb 01, 2012 2:04 pm
by RASHAD
Previous code updated to fix the X button

Re: Intercept the *Return Key*

Posted: Wed Feb 01, 2012 2:05 pm
by MachineCode
Randy, here's my gift to you. It's Windows-only, but works and won't need a workaround for the forseeable future.

Code: Select all

; ------------------------------------------------------------
;
;   PureBasic - Gadget example file
;
;    (c) 2002 - Fantaisie Software
;
; ------------------------------------------------------------
;

#WindowWidth  = 390
#WindowHeight = 350

If OpenWindow(0, 100, 200, #WindowWidth, #WindowHeight, "PureBasic - Gadget Demonstration", #PB_Window_MinimizeGadget)

  Top = 10
  GadgetHeight = 24

  Frame3DGadget(#PB_Any, 10, Top, 370, 290, "Player...") : Top+20

  StringGadget(0,  20, Top, 200, GadgetHeight, "")
  ButtonGadget(1, 223, Top,  72, GadgetHeight, "Play")
  ButtonGadget(2, 295, Top,  72, GadgetHeight, "Stop")  : Top+35
  DisableGadget(2,1)

  GadgetToolTip(1,"Play the current song")

  PanelGadget(3, 20, Top, #WindowWidth-50, #WindowHeight-Top-60)
    AddGadgetItem(3, 0, "MP3 PlayList")
      ListViewGadget(4, 6, 10, 230, 148)

      For k=0 To 30
        AddGadgetItem(4, -1, "Music Song n° "+Str(k))
      Next

      ButtonGadget(5,  250, 10, 80, GadgetHeight, "Add")
      ButtonGadget(6,  250, 38, 80, GadgetHeight, "Remove")
      ButtonGadget(7,  250, 66, 80, GadgetHeight, "Select")
      GadgetToolTip(7, "Select the current song")

      TrackBarGadget(17, 10, 168, 310, 25, 0, 100)

    AddGadgetItem(3, 1, "Options")
      Top = 10
      CheckBoxGadget(10, 10, Top, 250, GadgetHeight, "Enable low-pass filter") : Top+30
      CheckBoxGadget(11, 10, Top, 250, GadgetHeight, "Enable visual plug-in")  : Top+30
      ComboBoxGadget(12, 10, Top, 250, 21) : Top+30
        AddGadgetItem(12, -1, "FireWorks")
        AddGadgetItem(12, -1, "OpenGL spectrum")
        AddGadgetItem(12, -1, "Bump bass")
      SetGadgetState(12,0)
      DisableGadget(12,1)

      OptionGadget(13, 10, Top, 80, GadgetHeight, "640*480") : Top+20
      OptionGadget(14, 10, Top, 80, GadgetHeight, "800*600") : Top+20
      OptionGadget(15, 10, Top, 80, GadgetHeight, "1024*768")
      SetGadgetState(13, 1)

      ButtonGadget(16, 150, Top, 80, GadgetHeight, "Info")
  CloseGadgetList()

  TextGadget  (9, 10, #WindowHeight-30, 250, 24, "PureBasic - Gadget demonstration")
  ButtonGadget(8, #WindowWidth-100, #WindowHeight-36, 80, 24, "Quit")

  SetGadgetState(3, 0)

  Repeat

    EventID = WaitWindowEvent()

    If EventID = #WM_KEYDOWN And EventwParam() = #VK_RETURN ; Check if Enter is pressed.

      If GetFocus_() = GadgetID(0)
        MessageRequester("Info", "Return key pressed", 0)
        SetActiveGadget(0)
      EndIf

    ElseIf EventID = #PB_Event_Gadget

      Select EventGadget()

        Case 1 ; Play
          DisableGadget(2,0)  ; Enable the 'Stop' gadget
          DisableGadget(1,1)  ; Disable the 'Play' Gadget

        Case 2 ; Stop
          DisableGadget(1,0)  ; Enable the 'Play' gadget
          DisableGadget(2,1)  ; Disable the 'Stop' Gadget

        Case 4
          If EventType() = 2
            SetGadgetText(0, GetGadgetText(4)) ; Get the current item from the ListView..
          EndIf

        Case 5 ; Add
          AddGadgetItem(4, -1, "New Item Added...")

        Case 6 ; Remove
          RemoveGadgetItem(4, GetGadgetState(4)) ; Remove the current element of the ListView

        Case 7 ; Select
          SetGadgetText(0, GetGadgetText(4)) ; Get the current item from the ListView..

        Case 8 ; Quit...
          EventID = #PB_Event_CloseWindow

        Case 11 ; Enable PlugIn..
          DisableGadget(12, 1-GetGadgetState(11))

        Case 16 ;
          If GetGadgetState(13) : Result$ = GetGadgetText(13) : EndIf
          If GetGadgetState(14) : Result$ = GetGadgetText(14) : EndIf
          If GetGadgetState(15) : Result$ = GetGadgetText(15) : EndIf

          MessageRequester("Info", "Selected screen mode: "+Result$, 0)

        Case 17
          SetGadgetText(0, Str(GetGadgetState(17)))

      EndSelect

    EndIf

  Until EventID = #PB_Event_CloseWindow

EndIf

End

Re: Intercept the *Return Key*

Posted: Wed Feb 01, 2012 2:06 pm
by Randy Walker
RASHAD wrote:You do not need the close button because you have Quit Button :mrgreen:
:shock:
:lol: Hahahaha hahaah ha hahh ha hahh hahhahhhh hahh hahh ha ha hah hah huuugh hahaha, huhhgh, oooooewww
Really? :)

Re: Intercept the *Return Key*

Posted: Wed Feb 01, 2012 2:22 pm
by Randy Walker
RASHAD wrote:Previous code updated to fix the X button
You're giving honest effort to provide a solution, and I do appreciate the effort, but injecting more workaround solutions to code that gets broken emplimenting a previous workaround solution is whats gotten me so deep into this mess.

Can we get a show of hands from the audience please? How many of you would call this a workaround?

Code: Select all

          Case #PB_Event_CloseWindow
            Quit = 1
I just can't understand why the return key has such a discounted value in PB. For me, and most computer users I have ever encountered first hand, it seems like a pretty fundemental item. Soooo many apps, installations, utilities, ... on every OS platform I know. They all prompt the user to type a string of some sort and press enter to proceed, and do so in numerous places and in many instances, repeatedly.

Re: Intercept the *Return Key*

Posted: Wed Feb 01, 2012 2:45 pm
by RASHAD
Randy
What I did is not a workaround
It is straightforward programming using PB
The Return Key can not be used with one line Text Gadget what is the use?
No line feed permitted ,No new line permitted

OK next is your code exactly beside using the documented #PB_EventType_ReturnKey

Code: Select all


;
; ------------------------------------------------------------
;
;   PureBasic - Gadget example file
;
;    (c) 2002 - Fantaisie Software
;
; ------------------------------------------------------------
;

#WindowWidth  = 390
#WindowHeight = 350

If OpenWindow(0, 100, 200, #WindowWidth, #WindowHeight, "PureBasic - Gadget Demonstration", #PB_Window_MinimizeGadget)
    
  Top = 10
  GadgetHeight = 24

  Frame3DGadget(#PB_Any, 10, Top, 370, 290, "Player...") : Top+20

  StringGadget(0,  20, Top, 200, GadgetHeight, "",#ES_MULTILINE)
  ButtonGadget(1, 223, Top,  72, GadgetHeight, "Play")
  ButtonGadget(2, 295, Top,  72, GadgetHeight, "Stop")  : Top+35
  DisableGadget(2,1)
  
  GadgetToolTip(1,"Play the current song")
  
  PanelGadget(3, 20, Top, #WindowWidth-50, #WindowHeight-Top-60)
    AddGadgetItem(3, 0, "MP3 PlayList")
      ListViewGadget(4, 6, 10, 230, 148)

      For k=0 To 30
        AddGadgetItem(4, -1, "Music Song n° "+Str(k))
      Next

      ButtonGadget(5,  250, 10, 80, GadgetHeight, "Add")
      ButtonGadget(6,  250, 38, 80, GadgetHeight, "Remove")
      ButtonGadget(7,  250, 66, 80, GadgetHeight, "Select")
      GadgetToolTip(7, "Select the current song")
      
      TrackBarGadget(17, 10, 168, 310, 25, 0, 100)

    AddGadgetItem(3, 1, "Options")
      Top = 10
      CheckBoxGadget(10, 10, Top, 250, GadgetHeight, "Enable low-pass filter") : Top+30
      CheckBoxGadget(11, 10, Top, 250, GadgetHeight, "Enable visual plug-in")  : Top+30
      ComboBoxGadget(12, 10, Top, 250, 21) : Top+30
        AddGadgetItem(12, -1, "FireWorks")
        AddGadgetItem(12, -1, "OpenGL spectrum")
        AddGadgetItem(12, -1, "Bump bass")
      SetGadgetState(12,0)
      DisableGadget(12,1)
      
      OptionGadget(13, 10, Top, 80, GadgetHeight, "640*480") : Top+20
      OptionGadget(14, 10, Top, 80, GadgetHeight, "800*600") : Top+20
      OptionGadget(15, 10, Top, 80, GadgetHeight, "1024*768")
      SetGadgetState(13, 1)
      
      ButtonGadget(16, 150, Top, 80, GadgetHeight, "Info")
  CloseGadgetList()

  TextGadget  (9, 10, #WindowHeight-30, 250, 24, "PureBasic - Gadget demonstration")
  ButtonGadget(8, #WindowWidth-100, #WindowHeight-36, 80, 24, "Quit")

  SetGadgetState(3, 0)

  Repeat
    EventID = WaitWindowEvent()
    
    If EventID = #PB_Event_Gadget

      Select EventGadget()
        Case 0
          If EventType() = #PB_EventType_ReturnKey
            MessageRequester("Info", "Return key pressed", 0)
            SetActiveGadget(0)
          EndIf
          
        Case 1 ; Play
          DisableGadget(2,0)  ; Enable the 'Stop' gadget
          DisableGadget(1,1)  ; Disable the 'Play' Gadget
      
        Case 2 ; Stop
          DisableGadget(1,0)  ; Enable the 'Play' gadget
          DisableGadget(2,1)  ; Disable the 'Stop' Gadget
        
        Case 4
          If EventType() = 2
            SetGadgetText(0, GetGadgetText(4)) ; Get the current item from the ListView..
          EndIf

        Case 5 ; Add
          AddGadgetItem(4, -1, "New Item Added...")

        Case 6 ; Remove
          RemoveGadgetItem(4, GetGadgetState(4)) ; Remove the current element of the ListView

        Case 7 ; Select
          SetGadgetText(0, GetGadgetText(4)) ; Get the current item from the ListView..
  
        Case 8 ; Quit...
          EventID = #PB_Event_CloseWindow

        Case 11 ; Enable PlugIn..
          DisableGadget(12, 1-GetGadgetState(11))
          
        Case 16 ;
          If GetGadgetState(13) : Result$ = GetGadgetText(13) : EndIf
          If GetGadgetState(14) : Result$ = GetGadgetText(14) : EndIf
          If GetGadgetState(15) : Result$ = GetGadgetText(15) : EndIf
         
          MessageRequester("Info", "Selected screen mode: "+Result$, 0)
        
        Case 17
          SetGadgetText(0, Str(GetGadgetState(17)))
          
      EndSelect

    EndIf

  Until EventID = #PB_Event_CloseWindow

EndIf
End  

Re: Intercept the *Return Key*

Posted: Wed Feb 01, 2012 2:53 pm
by Randy Walker
MachineCode wrote:Randy, here's my gift to you. It's Windows-only...
Already employeed this workaround in my original code when I first ported over from GFA Basic to PureBasic. Since then I have been forced to add a CallBack and things just went downhill from there. Can't seem to rely on WaitWindowEvent() = #WM_KEYDOWN checking any more. So many events get clobbered in the callback (I HATE CallBacks) now I have to put in workaround patches to fix just about everything that wasn't broke... and I had it all working fine in PBv4.20 -- its not like I decided to start using CallBacks when I went to PBv4.60 and thats where 40% of my program operations got broken. Not just this Return key thing. My PopUpWindows are all broken. AddKeyboardShortcuts... all kinds of stuff went wonky moving from v4.2 to v4.60 with no modifications other than PB mandated change/dicards.

I forget where I came in exactly. My first upgrade was to v3.94 I think. That wasn't real painful. From there I went to v4.0 I think and up to 4.20 a while afterward. Don't recall which of those but one of them was pretty severely painful. Nothing like I'm seeing here now trying to go from v4.20 to v4.60 though.

Re: Intercept the *Return Key*

Posted: Wed Feb 01, 2012 3:25 pm
by em_uk
MachineCode wrote:AddKeyboardShortcut() is meant to be used with menus (even the manual says it), but people are using it as a general keyboard trapper and then losing functionality in other areas as a result.
Not verbatim :

Syntax

AddKeyboardShortcut(#Window, Shortcut, Event)
Description

Add or replace a keyboard shortcut to the specified #Window. A shortcut generates a menu event (like a menu item) as most of them are used in conjunction with menus. 'Event' is the number which will be returned by the EventMenu() function. The 'Event' value has a limited range, from 0 to 64000. By default, a window already has the #PB_Shortcut_Tab and #PB_Shortcut_Tab|#PB_Shortcut_Shift shortcuts to handle tab and shift-tab correctly trough the gadgets. A shortcut can be removed with RemoveKeyboardShortcut().

So we are OK to use AKS for this exact use.

Re: Intercept the *Return Key*

Posted: Wed Feb 01, 2012 5:06 pm
by kenmo
:?: :?: :?:

Randy, am I overlooking something...? Why can't you use AddKeyboardShortcut() with #PB_Shortcut_Return for your needs?

I have used PB's keyboard shortcuts for years (usually Return, Escape, and maybe F1), they always work and I think they are cross-platform. One of my current projects has maybe 10 different windows (not open at the same time of course!) and they all use Enter/Escape for fast alternatives to OK and Cancel buttons. (And they usually don't contain any actual "menus".)

I usually use it like this (pseudo-code obviously):

Code: Select all

AddKeyboardShortcut(MyWin, #PB_Shortcut_Return, #Enter)

; ... main event loop
; select Window event
case #PB_Event_Menu
; select Menu ID
case #Enter
; select GetActiveGadget()
; handle gadgets individually, OR
; run a verify/close procedure (as if an OK or Continue button was clicked)
; ...
The only problem is if your window has a multiline text gadget or editor gadget... how do you still receive Return presses? My preferred solution is to Remove and Add back the keyboard shortcut whenever the text gadget gains and loses focus. (It sounds like a workaround, but it is simple to implement and seems to work fine.)