Page 3 of 5

Re: Intercept the *Return Key*

Posted: Thu Feb 09, 2012 1:48 am
by netmaestro
And this is some silly workaround (or perceived shortcut) some people have gotten into the habit of doing:

Code: Select all

  Repeat
    Event = WaitWindowEvent()
    
    If Event = #PB_Event_CloseWindow  ; If the user has pressed on the close button
      quit = 1
    EndIf
    
  Until quit = 1
This is neither silly nor a workaround, but a solid coding technique the value of which you don't happen to understand. You might take a hint from the fact that many of the forum members that post code with this logic construct are some of the strongest coders here. Quite simply, the construct is designed to respond to multiple exits from the program, as most applications of any size at all will have several:

Code: Select all

Enumeration
  ; Menu items
  #MenuExit
  ; Gadgets
  #btnExit
EndEnumeration

OpenWindow(0,0,0,640,480,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
CreateMenu(0,WindowID(0))
MenuTitle("File")
MenuItem(#MenuExit, "Exit Program")
ButtonGadget(#btnExit, 300,430,80,20, "Exit Program")

quit=0
Repeat
  EventID = WaitWindowEvent()  
  Select EventID
    Case #PB_Event_CloseWindow
      quit=1
    Case #PB_Event_Menu
      Select EventMenu()
        Case #MenuExit
          quit=1
      EndSelect
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #btnExit
          quit=1
      EndSelect
  EndSelect
Until quit
"Silly workaround" is terminology that one shouldn't cast around lightly; it is well to think a thing through before saying something as ill-conceived as that. Also, your approach of changing the EventID received from WaitWindowEvent(), even though it might work, is illogical and convoluted and can lead to difficult-to-find bugs.

Re: Intercept the *Return Key*

Posted: Thu Feb 09, 2012 2:21 am
by kenmo
Randy Walker wrote:And this is some silly workaround (or perceived shortcut) some people have gotten into the habit of doing:
:? Silly workaround? Most coders use this method for one or more of these reasons:

(a) allows multiple exit conditions {as netmaestro pointed out}
(b) allows for different exit codes to be used {I sometimes use different constants for User-Quit, Error-Out, OS-Shutdown, etc.}
(c) it can be more robust {since various procedures/threads/routines can check for global exit codes}
(d) it is clean coding {easier to read and debug}.
Randy Walker wrote:If for some reason you encounter a condition that requires the application to close then this would be more appropriate:
:shock: Artificially changing the window event variable in the middle of your main program loop seems like a fast-track to confusion as your program grows in size and complexity. All it saves you is 4 bytes of memory!

Re: Intercept the *Return Key*

Posted: Thu Feb 09, 2012 3:03 am
by Randy Walker
netmaestro wrote:"Silly workaround" is terminology that one shouldn't cast around lightly; it is well to think a thing through before saying something as ill-conceived as that.
Sorry, don't mean to offend, but... I did think it through, and in my view, this...
..(perceived shortcut)...
does nothing to clarify or streamline the code.
netmaestro wrote: Also, your approach of changing the EventID, even though it might work, is illogical and convoluted and can lead to difficult-to-find bugs.
Taking into consideration your user selections are forcing a change of events, it seems more appropriate to me that your code should reflect that new directive and apply it to the variable in control of events directly? I think its just silly to juggle control from one vaiable to another when it is not necessary. In fact it only generates entropy. Never seen how clarity contrubutes to making bugs more ellusive. I can see how unnecessary juggling of control through a convoluted system of variables could lead to a troublesome debugging chore. Your code:

Code: Select all

quit=0
Repeat
  EventID = WaitWindowEvent() 
  Select EventID
    Case #PB_Event_CloseWindow
      quit=1
    Case #PB_Event_Menu
      Select EventMenu()
        Case #MenuExit
          quit=1
      EndSelect
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #btnExit
          quit=1
      EndSelect
  EndSelect
Until quit
My code:

Code: Select all

Repeat
  EventID = WaitWindowEvent() 
  Select EventID
    Case #PB_Event_Menu
      Select EventMenu()
        Case #MenuExit
          event = #PB_Event_CloseWindow
      EndSelect
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #btnExit
          event = #PB_Event_CloseWindow
      EndSelect
  EndSelect
Until event = #PB_Event_CloseWindow
3 lines shorter and certainly no loss of clarity. Given there are 3 less lines of entropy, I would conclude greater clarity in the latter sample.

Re: Intercept the *Return Key*

Posted: Thu Feb 09, 2012 3:36 am
by netmaestro
I have tried to help you and kenmo, a skilled coder and longtime forum member has expanded on that help. Ignoring us is your right of course, but bear in mind that obstinacy in cases like this can (and usually does) lead to advanced programmers seeing your name on a coding question and just moving on without opening it. Take my word for it: you can't afford to miss out on the help. Take a breath, sit back, grit your teeth if you must, but seriously- this is make-or-break for you. Just be chastised and leave it at that. Because every coder worth his salt reading this knows that you are full of shit. Do yourself a favor and don't piss them off. For crying out loud, this stuff is elementary.

Re: Intercept the *Return Key*

Posted: Thu Feb 09, 2012 3:58 am
by idle
netmaestro wrote: bear in mind that obstinacy in cases like this can (and usually does) lead to advanced programmers seeing your name on a coding question and just moving on without opening it.
Well read it and then move on!

Re: Intercept the *Return Key*

Posted: Thu Feb 09, 2012 4:01 am
by netmaestro
Well read it and then move on!
I thought I'd go the extra mile and try one more time. If that proves fruitless, which seems likely, that's what I'll do. But I thought it was worth explaining to the user, given his age, the impact his inexperienced and uneducated noobish chest-thumping can have.

Re: Intercept the *Return Key*

Posted: Thu Feb 09, 2012 4:08 am
by idle
netmaestro wrote:
Well read it and then move on!
I thought I'd go the extra mile and try one more time. If that proves fruitless, that's what I'll do.
You've always been willing to take one for the team, one of your best attributes.

Re: Intercept the *Return Key*

Posted: Thu Feb 09, 2012 4:23 am
by RASHAD
Randy
Why you said something like what you said
I myself when I post some code I know it is under when someone like NM say so
I think I am the one who started this and I still beleive that it is not a WORKAROUND
It is a matter of if you know your next step or you do not

Re: Intercept the *Return Key*

Posted: Thu Feb 09, 2012 5:11 am
by idle
modifying em_uk's example a little

Code: Select all

Enumeration 1
  #String1
  #String2
  #MenuQuit
EndEnumeration   

OpenWindow(0,#Null,#Null,400,200,"HAI",#PB_Window_ScreenCentered)
StringGadget(#String1,5,5,300,20,"Sting Field 1")
StringGadget(#String2,5,35,300,20,"2 Field String")
AddKeyboardShortcut(0,#PB_Shortcut_Escape,#menuQuit)

SetActiveGadget(#String1)

Repeat
  
  event=WaitWindowEvent()
     
  Select event
    Case #PB_Event_Gadget
      eventg=EventGadget()
      Select eventg
        Case #String1,#String2
          Select EventType()
            Case #PB_EventType_Focus
               AddKeyboardShortcut(0,#PB_Shortcut_Return, eventg)
            Case #PB_EventType_LostFocus
              RemoveKeyboardShortcut(0,#PB_Shortcut_Return)
          EndSelect
       EndSelect
    Case #PB_Event_Menu
      menu = EventMenu()
      Select Menu
         Case #String1,#String2
          MessageRequester("Easy",GetGadgetText(menu)+" See?")
        Case #MenuQuit 
           Quit=1
       EndSelect
     Case #PB_Event_CloseWindow
       Quit=1 
       RemoveKeyboardShortcut(0,#PB_Shortcut_Return)
  EndSelect

Until Quit=1

Re: Intercept the *Return Key*

Posted: Thu Feb 09, 2012 5:44 am
by netmaestro
Written off.

Re: Intercept the *Return Key*

Posted: Thu Feb 09, 2012 5:46 am
by idle
netmaestro wrote:Written off.
Give me a couple of more hours and I'm sure I'll be written off too!

Re: Intercept the *Return Key*

Posted: Thu Feb 09, 2012 5:54 am
by netmaestro
Try a year ago last August you eyeball you! Why can I even see you! MODS!!

Re: Intercept the *Return Key*

Posted: Thu Feb 09, 2012 5:58 am
by idle
netmaestro wrote:Try a year ago last August you eyeball you! Why can I even see you! MODS!!
Well that may be true but I was talking about my bottle of wine! :wink:

Re: Intercept the *Return Key*

Posted: Thu Feb 09, 2012 5:59 am
by Randy Walker
netmaestro wrote:
Well read it and then move on!
I thought I'd go the extra mile and try one more time. If that proves fruitless, which seems likely, that's what I'll do.
Hmmmm. Given the extra mile as you have, and without so much as a thanks, can't say I would blame you the least little bit. Sad oversight I on my part I openly admit. Frustrating to me too because so much of this is a monumental challenge to me. I am not a programmer. Can't figure out what a mesh is to save my life. Got to have a real programmer's brain to figure out more than half the stuff here in PureBasic alone, not to mention the API stuff. I get lost really easy in all that esoteric jargon. Clear simple logic on the other hand is, to me, clear and uncontestable. Maybe we don't use the same dictionary regarding the term ''workaround''. I'll have to open a thread in 'other topics' to hash that out if you care and dare to be annoyed :)

For now I think I should do justice to the title of this thread and close out with a working sample and acknowledgement to those that helped to refine the final solution.

(final post from Rashad: http://www.purebasic.fr/english/viewtop ... 00#p373544 )

Code: Select all

Global EventTyp.l, liveWin, lastLiveWin
#KB_Return = #PB_Shortcut_Return + 49152

Procedure KeyboardGyration()
  Select EventType()
    Case #PB_EventType_Focus
      Debug "focus on window " + Str(liveWin)
      If IsWindow(liveWin)
        AddKeyboardShortcut(liveWin, #PB_Shortcut_Return, #KB_Return)
      EndIf
      lastLiveWin = liveWin
    Case #PB_EventType_LostFocus
      Debug "lost focus on window " + Str(lastLiveWin)
      If IsWindow(lastLiveWin)
        RemoveKeyboardShortcut(lastLiveWin, #PB_Shortcut_Return)
      EndIf
  EndSelect
EndProcedure

OpenWindow(0,0,0,640,480,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)

TextGadget(#PB_Any, 10,20,100,16,"Enter Something:")
StringGadget(0, 110,20,300,20,"")
EditorGadget(1,10,50,400,400)
SetActiveGadget(0)

OpenWindow(2,0,0,640,480,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
TextGadget(#PB_Any, 10,20,100,16,"Enter Something:")
StringGadget(2, 110,20,300,20,"")
EditorGadget(3,10,50,400,400)
ButtonGadget(4,500,50,120,24,"Hide Window")
SetActiveGadget(2)

Repeat
  EventID = WaitWindowEvent()
  liveWin = GetActiveWindow()
  Select EventID
    Case #PB_Event_Gadget ;only one gadget list for all windows
      EventTyp.l = EventType()
      Select EventGadget()
        Case 0, 2
          KeyboardGyration()
        Case 4
          SetActiveGadget(2)
          HideWindow(2,1)
      EndSelect
    Case #PB_Event_Menu
      Select EventMenu()
        Case #KB_Return
          Select liveWin ;menu events can take place on different windows
            Case 0
              SetActiveGadget(1)
              HideWindow(2,0)
            Case 1
              SetActiveGadget(3)
          EndSelect
      EndSelect
  EndSelect
Until EventID = #PB_Event_CloseWindow
- - - and for their time, expertise, patients and generosity,
my thanks go out to:
netmaestro, Rashad, MachineCode, remi_meier, idle

- - - and others, for contributing their ideas and thoughts:
Tenaja, luis, IdeasVacuum, Foz

My humble thanks! :!:
Er, uh, well... humble as its going to get :lol:

Re: Intercept the *Return Key*

Posted: Thu Feb 09, 2012 6:04 am
by netmaestro
Well that may be true but I was talking about my bottle of wine!
I knew that. Just kidding of course, hehe. (omg, Scotty! Beam me up! Now would be a good time!)