How do I improve the keyboard response for this code ?

Just starting out? Need help? Post your questions and find answers here.
User avatar
pureballs
User
User
Posts: 41
Joined: Mon Oct 27, 2008 5:18 pm

How do I improve the keyboard response for this code ?

Post by pureballs »

It's a kind of HTML editor, just type html and hit CTRL-S and CTRL-E to switch between edit and html view.

Code: Select all

  
If OpenWindow(0, 0, 0, 600, 300, "CTRL-S for SHOW - CTRL-E for EDIT", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0))
    WebGadget(0, 0, 0, 600, 300, "")
    HideGadget(0,1)
    StringGadget(1,0,0,600,300,"",#PB_String_BorderLess | #ES_MULTILINE | #ES_AUTOHSCROLL | #ES_AUTOVSCROLL |  #ESB_DISABLE_LEFT| #ESB_DISABLE_RIGHT)
    SetActiveGadget(1)
    
    If LoadFont(1,"MS Sans Serif",12)
     SetGadgetFont(1, FontID(1))  
    EndIf
    
    AddKeyboardShortcut(0, #PB_Shortcut_Control | #PB_Shortcut_S, 1)
    AddKeyboardShortcut(0, #PB_Shortcut_Control | #PB_Shortcut_E, 2)
    
  Repeat 
  
  If WaitWindowEvent()= #PB_Event_Menu
      
      If EventMenu()=1
       HideGadget(1,1)
       HideGadget(0,0)
       SetActiveGadget(0)
          
       text$=GetGadgetText(1)
      
       new$="<html><body topmargin=0 leftmargin=0><font face='MS Sans Serif' size=3pc>"
       new$ + ReplaceString(text$,Chr(13)+Chr(10),"<br>")
       
       SetGadgetItemText(0,#PB_Web_HtmlCode,new$)
      EndIf
    
      If EventMenu()=2
        HideGadget(1,0)
        HideGadget(0,1)
        SetActiveGadget(1)
      EndIf
   
  EndIf
     
  Until WaitWindowEvent() = #PB_Event_CloseWindow
   
  EndIf : End
As you can see, you need to press 2 or 3 times to make the program respond, I tried delays at different spots, removed them as they didn't improve response at all.

Also, there's a difference between the EDIT and HTML screens, the HTML screen has (with little input disabled) vertical scrollbar, as the EDIT window has not. Hot to add the Vscrollbar for the EDIT window ?

Cheers.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Code: Select all

If OpenWindow(0, 0, 0, 600, 300, "CTRL-S for SHOW - CTRL-E for EDIT", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0)) 
    WebGadget(0, 0, 0, 600, 300, "") 
    HideGadget(0,1) 
    StringGadget(1,0,0,600,300,"",#PB_String_BorderLess | #ES_MULTILINE | #ES_AUTOHSCROLL | #ES_AUTOVSCROLL |  #ESB_DISABLE_LEFT| #ESB_DISABLE_RIGHT) 
    SetActiveGadget(1) 
    
    If LoadFont(1,"MS Sans Serif",12) 
     SetGadgetFont(1, FontID(1))  
    EndIf 
    
    AddKeyboardShortcut(0, #PB_Shortcut_Control | #PB_Shortcut_S, 1) 
    AddKeyboardShortcut(0, #PB_Shortcut_Control | #PB_Shortcut_E, 2) 
    
  Repeat 
    EventID = WaitWindowEvent()
    Select EventID
      Case #PB_Event_Menu 
        If EventMenu()=1 
          HideGadget(1,1) 
          HideGadget(0,0) 
          SetActiveGadget(0) 
          
          text$=GetGadgetText(1) 
      
          new$="<html><body topmargin=0 leftmargin=0><font face='MS Sans Serif' size=3pc>" 
          new$ + ReplaceString(text$,Chr(13)+Chr(10),"<br>") 
        
          SetGadgetItemText(0,#PB_Web_HtmlCode,new$) 
        ElseIf EventMenu()=2 
          HideGadget(1,0) 
          HideGadget(0,1) 
          SetActiveGadget(1) 
        EndIf 
    
  EndSelect 
      
  Until EventID = #PB_Event_CloseWindow 
    
  EndIf : End
Your problem was that you were using too many WaitWindowEvent()'s (a common problem!) and consequently some events were going unprocessed.
I may look like a mule, but I'm not a complete ass.
Baldrick
Addict
Addict
Posts: 860
Joined: Fri Jul 02, 2004 6:49 pm
Location: Australia

Post by Baldrick »

Havent looked closely at ur code, but did notice you had 2 WaitwindowEvent()'s in ur loop, which is pretty much a bit of a no no.
Change ur loop to the below & see if it helps:

Code: Select all

  Repeat 
  
  Event.l= WaitWindowEvent() 
   If Event = #PB_Event_Menu 
    Select EventMenu() 
     Case 1 
      HideGadget(1,1) 
      HideGadget(0,0) 
      SetActiveGadget(0)  
      text$=GetGadgetText(1) 
      new$="<html><body topmargin=0 leftmargin=0><font face='MS Sans Serif' size=3pc>" 
      new$ + ReplaceString(text$,Chr(13)+Chr(10),"<br>")   
      SetGadgetItemText(0,#PB_Web_HtmlCode,new$)
     Case 2 
      HideGadget(1,0) 
      HideGadget(0,1) 
      SetActiveGadget(1)
    EndSelect 
   EndIf 
      
  Until Event = #PB_Event_CloseWindow 
Edit: Beaten by the srod again.. 8)
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

hehe!!! :)
I may look like a mule, but I'm not a complete ass.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

everytime I read something like
you need to press 2 or 3 times to make the program respond
I first look into the code if there is more than one (Wait)WindowEvent(),
and heck, they were there everytime.
oh... and have a nice day.
User avatar
pureballs
User
User
Posts: 41
Joined: Mon Oct 27, 2008 5:18 pm

Post by pureballs »

Serious competition going on right here ! :D

Thanks... I knew it had to be the case select thing that I avoided from the start because I thought it would be faster !?!
What was I thinking ? :)

Now moving on to adding a Vscrollbar for the String Gadget... I have so many flags allready for that gadget, the more you have. the bigger the chance they do interact in an undesirable way.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Just add #WS_VSCROLL to your list of flags.
I may look like a mule, but I'm not a complete ass.
User avatar
pureballs
User
User
Posts: 41
Joined: Mon Oct 27, 2008 5:18 pm

Post by pureballs »

srod wrote:Just add #WS_VSCROLL to your list of flags.
thanks...

used the wrong (#PB) flag.
Post Reply