Page 1 of 1

send tab or shit+tab to window in linux and mac

Posted: Fri Oct 28, 2016 2:20 pm
by Peyman
hi, as i write a custom string gadget http://purebasic.fr/english/viewtopic.php?f=12&t=66815 its have a problem in linux and mac, pb didnt send Tab and Shit+Tab automatically to window as in windows it do it, so how can i send it manually to window ? i can get tab and shit tab in keydown event but cant send it to window in linux and mac

thx

EDIT :
sry i write SHIT instead of SHIFT, its just a typo problem :lol: :lol:
edited.

Code: Select all

Procedure CanvasCallback()
  Protected Text$ = "I have Focus use TAB or SHIFT + TAB"

  Select EventType()
    Case #PB_EventType_Focus
      StartDrawing(CanvasOutput(EventGadget()))
        DrawText((OutputWidth() - TextWidth(Text$)) / 2, (OutputHeight() - TextHeight(Text$)) / 2, Text$, 0, $FFFFFF)
      StopDrawing()
      
    Case #PB_EventType_LostFocus
      StartDrawing(CanvasOutput(EventGadget()))
        Box(0, 0, OutputWidth(), OutputHeight(), $FFFFFF)
      StopDrawing()
  EndSelect  
EndProcedure


If OpenWindow(0, 0, 0, 400, 230, "TAB and SHIFT + TAB in linux and mac", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    CanvasGadget(0, 20, 20, 300, 30, #PB_Canvas_Keyboard)
    CanvasGadget(1, 20, 60, 300, 30, #PB_Canvas_Keyboard)
    CanvasGadget(2, 20, 100, 300, 30, #PB_Canvas_Keyboard)
    CanvasGadget(3, 20, 140, 300, 30, #PB_Canvas_Keyboard)
    CanvasGadget(4, 20, 180, 300, 30, #PB_Canvas_Keyboard)
    
    BindGadgetEvent(0, @CanvasCallback())
    BindGadgetEvent(1, @CanvasCallback())
    BindGadgetEvent(2, @CanvasCallback())
    BindGadgetEvent(3, @CanvasCallback())
    BindGadgetEvent(4, @CanvasCallback())
    
    SetActiveGadget(0)
    
    Repeat
      Event = WaitWindowEvent()
    Until Event = #PB_Event_CloseWindow
  EndIf


Re: send tab or shit+tab to window in linux and mac

Posted: Fri Oct 28, 2016 2:59 pm
by RASHAD
Did you tried AddKeyboardShortcut()?

Code: Select all

   CompilerIf #PB_Compiler_OS <> #PB_OS_Windows
      AddKeyboardShortcut(0,#PB_Shortcut_Tab,10)
      AddKeyboardShortcut(0,#PB_Shortcut_Tab|#PB_Shortcut_Shift ,20)
    CompilerEndIf

Re: send tab or shit+tab to window in linux and mac

Posted: Fri Oct 28, 2016 5:35 pm
by Peyman
@Rashad,

no did not test it, anyway its not work as i test it now in ubuntu.

Re: send tab or shit+tab to window in linux and mac

Posted: Fri Oct 28, 2016 6:14 pm
by RASHAD
Try the next with Linux & MAC
If it is fine then adapt it into your code

Code: Select all

If OpenWindow(0, 0, 0, 400, 230, "TAB and SHIFT + TAB in linux and mac", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    CanvasGadget(0, 20, 20, 300, 30, #PB_Canvas_Keyboard)
    CanvasGadget(1, 20, 60, 300, 30, #PB_Canvas_Keyboard)
    CanvasGadget(2, 20, 100, 300, 30, #PB_Canvas_Keyboard)
    CanvasGadget(3, 20, 140, 300, 30, #PB_Canvas_Keyboard)
    CanvasGadget(4, 20, 180, 300, 30, #PB_Canvas_Keyboard)   
  
    SetActiveGadget(0)
    Text$ = "I have Focus use TAB or SHIT + TAB"
    StartDrawing(CanvasOutput(0))
      DrawText((OutputWidth() - TextWidth(Text$)) / 2, (OutputHeight() - TextHeight(Text$)) / 2, Text$, 0, $FFFFFF)
    StopDrawing()
   
    AddKeyboardShortcut(0,#PB_Shortcut_Tab,10)
    AddKeyboardShortcut(0,#PB_Shortcut_Tab|#PB_Shortcut_Shift ,20)
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Quit = 1
         
        Case #PB_Event_Menu
          Select EventMenu()
            Case 10
                Text$ = "I have Focus use TAB or SHIT + TAB"
                StartDrawing(CanvasOutput(GetActiveGadget()))
                  Box(0, 0, OutputWidth(), OutputHeight(), $FFFFFF)
                StopDrawing()
                If GetActiveGadget() = 4
                  SetActiveGadget(0)
                Else
                  SetActiveGadget(GetActiveGadget()+1)
                EndIf
                StartDrawing(CanvasOutput(GetActiveGadget()))
                  DrawText((OutputWidth() - TextWidth(Text$)) / 2, (OutputHeight() - TextHeight(Text$)) / 2, Text$, 0, $FFFFFF)
                StopDrawing()
                
            Case 20
                Text$ = "I have Focus use TAB or SHIT + TAB"
                StartDrawing(CanvasOutput(GetActiveGadget()))
                  Box(0, 0, OutputWidth(), OutputHeight(), $FFFFFF)
                StopDrawing()
                If GetActiveGadget() = 0
                  SetActiveGadget(4)
                Else
                  SetActiveGadget(GetActiveGadget()-1) 
                EndIf
                StartDrawing(CanvasOutput(GetActiveGadget()))
                  DrawText((OutputWidth() - TextWidth(Text$)) / 2, (OutputHeight() - TextHeight(Text$)) / 2, Text$, 0, $FFFFFF)
                StopDrawing()
                
          EndSelect
      EndSelect
    Until Quit = 1
  EndIf
Edit :Modified for optimization

Re: send tab or shit+tab to window in linux and mac

Posted: Fri Oct 28, 2016 7:17 pm
by Peyman
thx Rashad nice trick GetActiveGadget()+1 its works, but i cant use this way, because i dont know the number of my string gadgets and maybe gadgets created with #Pb_Any parameter. there is no api in linux and mac for simulating keyborad as windows like keybd_event_ ? or any way that is more dynamic.

Re: send tab or shit+tab to window in linux and mac

Posted: Fri Oct 28, 2016 7:34 pm
by RASHAD
Do not give up :)

Code: Select all

Global Dim gad(4)

If OpenWindow(0, 0, 0, 400, 230, "TAB and SHIFT + TAB in linux and mac", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    gad(0) = CanvasGadget(#PB_Any, 20, 20, 300, 30, #PB_Canvas_Keyboard)
    gad(1) = CanvasGadget(#PB_Any, 20, 60, 300, 30, #PB_Canvas_Keyboard)
    gad(2) = CanvasGadget(#PB_Any, 20, 100, 300, 30, #PB_Canvas_Keyboard)
    gad(3) = CanvasGadget(#PB_Any, 20, 140, 300, 30, #PB_Canvas_Keyboard)
    gad(4) = CanvasGadget(#PB_Any, 20, 180, 300, 30, #PB_Canvas_Keyboard) 
    
    SetActiveGadget(gad(0))
    Text$ = "I have Focus use TAB or SHIT + TAB"
    StartDrawing(CanvasOutput(gad(0)))
      DrawText((OutputWidth() - TextWidth(Text$)) / 2, (OutputHeight() - TextHeight(Text$)) / 2, Text$, 0, $FFFFFF)
    StopDrawing()
   
    AddKeyboardShortcut(0,#PB_Shortcut_Tab,10)
    AddKeyboardShortcut(0,#PB_Shortcut_Tab|#PB_Shortcut_Shift ,20)
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Quit = 1
         
        Case #PB_Event_Menu
          Select EventMenu()
            Case 10
                Text$ = "I have Focus use TAB or SHIT + TAB"
                StartDrawing(CanvasOutput(GetActiveGadget()))
                  Box(0, 0, OutputWidth(), OutputHeight(), $FFFFFF)
                StopDrawing()
                If GetActiveGadget() = gad(4)
                  SetActiveGadget(gad(0))
                Else
                  For x = 0 To 4
                    If GetActiveGadget() = gad(x)                    
                      SetActiveGadget(gad(x+1))
                      Break
                    EndIf
                  Next
                EndIf
                StartDrawing(CanvasOutput(GetActiveGadget()))
                  DrawText((OutputWidth() - TextWidth(Text$)) / 2, (OutputHeight() - TextHeight(Text$)) / 2, Text$, 0, $FFFFFF)
                StopDrawing()
               
            Case 20
                Text$ = "I have Focus use TAB or SHIT + TAB"
                StartDrawing(CanvasOutput(GetActiveGadget()))
                  Box(0, 0, OutputWidth(), OutputHeight(), $FFFFFF)
                StopDrawing()
                If GetActiveGadget() = gad(0)
                  SetActiveGadget(gad(4))
                Else
                  For x = 0 To 4
                    If GetActiveGadget() = gad(x)                    
                      SetActiveGadget(gad(x-1))
                      Break
                    EndIf
                  Next
                EndIf
                StartDrawing(CanvasOutput(GetActiveGadget()))
                  DrawText((OutputWidth() - TextWidth(Text$)) / 2, (OutputHeight() - TextHeight(Text$)) / 2, Text$, 0, $FFFFFF)
                StopDrawing()
               
          EndSelect
      EndSelect
    Until Quit = 1
  EndIf

Re: send tab or shit+tab to window in linux and mac

Posted: Sun Oct 30, 2016 12:29 pm
by Peyman
thx RASHAD, its a good way if i write a program for my self, i write a module that create Custom StringGadget for programmers, i think its dirty that i write a procedure in module and force programmers to send all gadget numbers that they create to it just for TAB and SHIFT + TAB, i dont know really how to solve it, its a bug in pb for linux and mac or works in windows just by luck ?

Re: send tab or shit+tab to window in linux and mac

Posted: Sun Oct 30, 2016 2:06 pm
by mk-soft
Perhaps help this advance functions for previous and next gadget

Update v1.05
- Changes as Module

Code: Select all

;-TOP

; Comment: Module Advanced Gadget Functions
; Author : mk-soft
; Version: v1.05
; Created: 30.10.2016
; Updated: 
; Link   : 
;

; *****************************************************************************

DeclareModule AGF ; Advanced Gadget Functions
  
  Declare GetParentWindowID(Gadget)
  Declare GetPreviousGadget(Gadget, WindowID)
  Declare GetNextGadget(Gadget, WindowID)
  
EndDeclareModule

; ---------------------------------------------------------------------------

Module AGF ; Advanced Gadget Functions
  
  EnableExplicit
  
  ; Import internal function ------------------------------------------------------------
  
  CompilerIf #PB_Compiler_OS = #PB_OS_Windows
    Import ""
      PB_Object_EnumerateStart( PB_Objects )
      PB_Object_EnumerateNext( PB_Objects, *ID.Integer )
      PB_Object_EnumerateAbort( PB_Objects )
      PB_Object_GetObject( PB_Object , DynamicOrArrayID)
      PB_Window_Objects.i
      PB_Gadget_Objects.i
    EndImport
  CompilerElse
    ImportC ""
      PB_Object_EnumerateStart( PB_Objects )
      PB_Object_EnumerateNext( PB_Objects, *ID.Integer )
      PB_Object_EnumerateAbort( PB_Objects )
      PB_Object_GetObject( PB_Object , DynamicOrArrayID)
      PB_Window_Objects.i
      PB_Gadget_Objects.i
    EndImport
  CompilerEndIf
  
  CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
    ; PB Interne Struktur Gadget MacOS
    Structure sdkGadget
      *gadget
      *container
      *vt
      UserData.i
      Window.i
      Type.i
      Flags.i
    EndStructure
  CompilerEndIf

  ; ---------------------------------------------------------------------------
  
  Procedure GetParentWindowID(Gadget)
    Protected GadgetID, GadgetWindowID
    
    If IsGadget(Gadget)
      CompilerSelect #PB_Compiler_OS 
        CompilerCase #PB_OS_MacOS
          Protected *Gadget.sdkGadget = IsGadget(Gadget)
          GadgetWindowID = WindowID(*Gadget\Window)
        CompilerCase #PB_OS_Linux 
          GadgetID = GadgetID(Gadget)
          GadgetWindowID = gtk_widget_get_toplevel_ (GadgetID)
        CompilerCase #PB_OS_Windows
          GadgetID = GadgetID(Gadget)
          While GadgetID 
            GadgetWindowID = GadgetID : GadgetID = GetParent_( GadgetID )
          Wend 
      CompilerEndSelect
    EndIf
    ProcedureReturn GadgetWindowID
  EndProcedure
 
  ; ---------------------------------------------------------------------------
  
  Procedure GetPreviousGadget(Gadget, WindowID)
    Protected object, prev_id, type
    
    prev_id = -1
    PB_Object_EnumerateStart(PB_Gadget_Objects)
    While PB_Object_EnumerateNext(PB_Gadget_Objects, @object)
      type = GadgetType(object)
      If type <> #PB_GadgetType_Text And type <> #PB_GadgetType_Frame
        If GetParentWindowID(object) = WindowID
          If gadget = object
            If prev_id > 0
              PB_Object_EnumerateAbort(PB_Gadget_Objects)
              Break
            EndIf
          Else
            prev_id = object
          EndIf
        EndIf
      EndIf
    Wend
    ProcedureReturn prev_id
  EndProcedure
  
  ; ---------------------------------------------------------------------------
  
  Procedure GetNextGadget(Gadget, WindowID)
    Protected object, next_id, type
    
    next_id = -1
    PB_Object_EnumerateStart(PB_Gadget_Objects)
    While PB_Object_EnumerateNext(PB_Gadget_Objects, @object)
      type = GadgetType(object)
      If type <> #PB_GadgetType_Text And type <> #PB_GadgetType_Frame
        If GetParentWindowID(object) = WindowID
          If next_id < 0
            next_id = object
          EndIf
          If gadget = object
            If PB_Object_EnumerateNext(PB_Gadget_Objects, @object)
              If GetParentWindowID(object) = WindowID
                next_id = object
                PB_Object_EnumerateAbort(PB_Gadget_Objects)
                Break
              EndIf
            EndIf
          EndIf
        EndIf
      EndIf
    Wend
    ProcedureReturn next_id
  EndProcedure
  
EndModule

; *****************************************************************************

;- Example

CompilerIf #PB_Compiler_IsMainFile
  
  UseModule AGF
  
  Global Dim gad(4)
  
  If OpenWindow(0, 0, 0, 400, 230, "TAB and SHIFT + TAB in linux and mac", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    gad(0) = CanvasGadget(#PB_Any, 20, 20, 300, 30, #PB_Canvas_Keyboard)
    gad(1) = CanvasGadget(#PB_Any, 20, 60, 300, 30, #PB_Canvas_Keyboard)
    gad(2) = CanvasGadget(#PB_Any, 20, 100, 300, 30, #PB_Canvas_Keyboard)
    gad(3) = CanvasGadget(#PB_Any, 20, 140, 300, 30, #PB_Canvas_Keyboard)
    gad(4) = CanvasGadget(#PB_Any, 20, 180, 300, 30, #PB_Canvas_Keyboard) 
    
    SetActiveGadget(gad(0))
    Text$ = "I have Focus use TAB or SHIT + TAB"
    StartDrawing(CanvasOutput(gad(0)))
      DrawText((OutputWidth() - TextWidth(Text$)) / 2, (OutputHeight() - TextHeight(Text$)) / 2, Text$, 0, $FFFFFF)
    StopDrawing()
   
    AddKeyboardShortcut(0,#PB_Shortcut_Tab,10)
    AddKeyboardShortcut(0,#PB_Shortcut_Tab|#PB_Shortcut_Shift ,20)
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Quit = 1
         
        Case #PB_Event_Menu
          Select EventMenu()
            Case 10
                Text$ = "I have Focus use TAB or SHIT + TAB"
                StartDrawing(CanvasOutput(GetActiveGadget()))
                  Box(0, 0, OutputWidth(), OutputHeight(), $FFFFFF)
                StopDrawing()
                
                SetActiveGadget(GetNextGadget(GetActiveGadget(), WindowID(GetActiveWindow())))
                
                StartDrawing(CanvasOutput(GetActiveGadget()))
                  DrawText((OutputWidth() - TextWidth(Text$)) / 2, (OutputHeight() - TextHeight(Text$)) / 2, Text$, 0, $FFFFFF)
                StopDrawing()
               
            Case 20
                Text$ = "I have Focus use TAB or SHIT + TAB"
                StartDrawing(CanvasOutput(GetActiveGadget()))
                  Box(0, 0, OutputWidth(), OutputHeight(), $FFFFFF)
                StopDrawing()
                
                SetActiveGadget(GetPreviousGadget(GetActiveGadget(), WindowID(GetActiveWindow())))
                
                StartDrawing(CanvasOutput(GetActiveGadget()))
                  DrawText((OutputWidth() - TextWidth(Text$)) / 2, (OutputHeight() - TextHeight(Text$)) / 2, Text$, 0, $FFFFFF)
                StopDrawing()
               
          EndSelect
      EndSelect
    Until Quit = 1
  EndIf
  
CompilerEndIf

Re: send tab or shit+tab to window in linux and mac

Posted: Sun Oct 30, 2016 5:54 pm
by Peyman
@mk-soft,

thx so much this is exactly what i wanted, can i merge this code in my module and write a thx to u in module description ?

Re: send tab or shit+tab to window in linux and mac

Posted: Sun Oct 30, 2016 8:09 pm
by mk-soft
You can use it as a module...

Show Update v1.03 :wink:

Re: send tab or shit+tab to window in linux and mac

Posted: Sun Oct 30, 2016 11:29 pm
by Peyman
@mk-soft,

thx for module so i reference it in the code if users want so they can use it, please create a post in Tricks 'n' Tips and post your module.
another thing in line 97 of your code logic is not true because gadget number can be zero so it must be Greater Equal to 0

Code: Select all

...
If prev_id >= 0
...

Re: send tab or shit+tab to window in linux and mac

Posted: Mon Oct 31, 2016 1:01 am
by mk-soft
That´s right