Page 1 of 1

Positioning the cursor

Posted: Sat Sep 14, 2024 5:55 pm
by ClueLess
Hi

How do I force Positioning the cursor in a specific String box when I press the tab key?

Thank you

Re: Positioning the cursor

Posted: Sat Sep 14, 2024 7:08 pm
by Quin
Do you mean whenever the user tabs into your StringGadget specifically, or around the app in general?
As far as setting the cursor goes, that really depends on your platform. On Windows, for example, you'd do something like:

Code: Select all

SendMessage_(GadgetID(#Gadget), #EM_SETSEL, startpos, endpos)

Re: Positioning the cursor

Posted: Sat Sep 14, 2024 7:13 pm
by boddhi
The most simpliest and cross-platform if I've well understood your request:

Code: Select all

If OpenWindow(0, 0, 0, 322, 205, "Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  StringGadget(0, 8,  10, 306, 20, "")
  StringGadget(1, 8,  35, 306, 20, "")
  StringGadget(2, 8,  60, 306, 20, "")
  AddKeyboardShortcut(0,#PB_Shortcut_Tab,500)
  Repeat
    Event= WaitWindowEvent()
    Select Event
      Case #PB_Event_Menu
        Select EventMenu()
          Case 500
            Select GetActiveGadget()
              Case 0:NewGadget=2
              Case 2:NewGadget=1
              Default:NewGadget=0
            EndSelect
            SetActiveGadget(NewGadget)
        EndSelect
    EndSelect
  Until event = #PB_Event_CloseWindow
EndIf
@Quin
At first, I thought too that was the request, but I'm not sure. :wink:

Re: Positioning the cursor

Posted: Sat Sep 14, 2024 8:19 pm
by Quin
boddhi wrote: Sat Sep 14, 2024 7:13 pm @Quin
At first, I thought too that was the request, but I'm not sure. :wink:
I started reconsidering too after I made my first post. The wording is slightly ambiguous
@OP, would you mind telling us a bit more about what you're actually trying to do here?

Re: Positioning the cursor

Posted: Sat Sep 14, 2024 11:25 pm
by minimy
This may be a solution

Code: Select all

;{ this is part of code maked by Axoloti here: https://www.purebasic.fr/english/viewtopic.php?t=85314
Macro IsKeyPressed(VirtualKey)  ; BOOL .. on #VK_SHIFT, #VK_CONTROL, etc. 
  Bool(GetAsyncKeyState_(VirtualKey) & $8000) 
EndMacro 
;}

;{ 
winMain= OpenWindow(#PB_Any,0,0,960,540,"TAB   was a drink?",#PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
For p= 1 To 5
  StringGadget(p,10,15+p*35,200,25,"String "+Str(p))
Next p

Repeat
event= WindowEvent()

  Select event
    Case #PB_Event_Gadget
      EventGadget=  EventGadget()
      EventType=    EventType()
      Select EventGadget
        Case 0
          
      EndSelect
      
    Case #PB_Event_CloseWindow
      Break 
  EndSelect
  
  If IsKeyPressed(#VK_TAB) And release <> #VK_TAB
    release=#VK_TAB
  ElseIf IsKeyPressed(#VK_TAB)=0 And release= #VK_TAB
    release= 0
    Debug "TAB active gadget 4"
    SetActiveGadget(4)
  EndIf
  
  Delay(1)
  
ForEver
;}

Re: Positioning the cursor

Posted: Sun Sep 15, 2024 3:07 am
by RASHAD
For Windows

Code: Select all


LoadFont(0,"Georgia",16)

OpenWindow(0,0,0,600,200,"Control TABSTOPS",#PB_Window_SystemMenu | #PB_Window_MinimizeGadget| #PB_Window_ScreenCentered)
StringGadget(0,10,10,580,30,"", #ES_MULTILINE ) 

Dim tabStops.i(4)
tabStops(0) =  12
tabStops(1) =  36
tabStops(2) =  80
tabStops(3) =  140
SendMessage_(GadgetID(0),#EM_SETTABSTOPS,ArraySize(tabStops()),@tabStops())

SetGadgetFont(0,FontID(0))
SetActiveGadget(0)

AddKeyboardShortcut(0,#PB_Shortcut_Tab,10)

Repeat
  Select WaitWindowEvent()
      
    Case #PB_Event_CloseWindow
      Quit = 1
      
    Case #PB_Event_Menu
      Select EventMenu()
        Case 10
          If GetActiveGadget() = 0
            SendMessage_(GadgetID(0),#WM_CHAR,9,0)
          EndIf
      EndSelect   
      
  EndSelect
Until Quit = 1
End  



Re: Positioning the cursor

Posted: Sun Sep 15, 2024 7:37 am
by TI-994A
Expanding on @boddhi's example, the tab order can be arranged as required:

Code: Select all

#stringTab = 99

window = OpenWindow(#PB_Any, 0, 0, 590, 220, "Textbox Tab Order",
                    #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

; sequential order
s1 = StringGadget(#PB_Any, 30, 30, 150, 30, "1. first tab")
s2 = StringGadget(#PB_Any, 220, 30, 150, 30, "2. second tab")
s3 = StringGadget(#PB_Any, 410, 30, 150, 30, "3. third tab")

; reverse order
s4 = StringGadget(#PB_Any, 30, 90, 150, 30, "6. sixth tab ")
s5 = StringGadget(#PB_Any, 220, 90, 150, 30, "5. fifth tab")
s6 = StringGadget(#PB_Any, 410, 90, 150, 30, "4. fourth tab")

; sequential order
s7 = StringGadget(#PB_Any, 30, 150, 150, 30, "7. seventh tab")
s8 = StringGadget(#PB_Any, 220, 150, 150, 30, "8. eighth tab")
s9 = StringGadget(#PB_Any, 410, 150, 150, 30, "9. ninth tab")
AddKeyboardShortcut(window ,#PB_Shortcut_Tab, #stringTab)

SetGadgetData(s1, s2)   ; from string 1 jump to string 2
SetGadgetData(s2, s3)   ; from string 2 jump to string 3
SetGadgetData(s3, s6)   ; from string 3 jump to string 6
SetGadgetData(s4, s7)   ; from string 4 jump to string 7
SetGadgetData(s5, s4)   ; from string 5 jump to string 4
SetGadgetData(s6, s5)   ; from string 6 jump to string 5
SetGadgetData(s7, s8)   ; from string 7 jump to string 8
SetGadgetData(s8, s9)   ; from string 8 jump to string 9
SetGadgetData(s9, s1)   ; from string 9 jump to string 1

SetActiveGadget(s1)

Repeat
  event= WaitWindowEvent()
  Select event
    Case #PB_Event_CloseWindow
      appQuit = #True
    Case #PB_Event_Menu
      Select EventMenu()
        Case #stringTab
          nextTab = GetGadgetData(GetActiveGadget())
          SetActiveGadget(nextTab)        
      EndSelect
  EndSelect
Until appQuit

And here, in a different order:

Code: Select all

#stringTab = 99

window = OpenWindow(#PB_Any, 0, 0, 590, 220, "Textbox Tab Order",
                    #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

s1 = StringGadget(#PB_Any, 30, 30, 150, 30, "1. first tab")
s2 = StringGadget(#PB_Any, 220, 30, 150, 30, "6. sixth tab ")
s3 = StringGadget(#PB_Any, 410, 30, 150, 30, "7. seventh tab")
s4 = StringGadget(#PB_Any, 30, 90, 150, 30, "2. second tab")
s5 = StringGadget(#PB_Any, 220, 90, 150, 30, "5. fifth tab")
s6 = StringGadget(#PB_Any, 410, 90, 150, 30, "8. eighth tab")
s7 = StringGadget(#PB_Any, 30, 150, 150, 30, "3. third tab")
s8 = StringGadget(#PB_Any, 220, 150, 150, 30, "4. fourth tab")
s9 = StringGadget(#PB_Any, 410, 150, 150, 30, "9. ninth tab")
AddKeyboardShortcut(window ,#PB_Shortcut_Tab, #stringTab)

SetGadgetData(s1, s4)   ; from string 1 jump to string 4
SetGadgetData(s2, s3)   ; from string 2 jump to string 3
SetGadgetData(s3, s6)   ; from string 3 jump to string 6
SetGadgetData(s4, s7)   ; from string 4 jump to string 7
SetGadgetData(s5, s2)   ; from string 5 jump to string 2
SetGadgetData(s6, s9)   ; from string 6 jump to string 9
SetGadgetData(s7, s8)   ; from string 7 jump to string 8
SetGadgetData(s8, s5)   ; from string 8 jump to string 5
SetGadgetData(s9, s1)   ; from string 9 jump to string 1

SetActiveGadget(s1)

Repeat
  event= WaitWindowEvent()
  Select event
    Case #PB_Event_CloseWindow
      appQuit = #True
    Case #PB_Event_Menu
      Select EventMenu()
        Case #stringTab
          nextTab = GetGadgetData(GetActiveGadget())
          SetActiveGadget(nextTab)        
      EndSelect
  EndSelect
Until appQuit

And here, with reverse tabbing using the Shift-Tab key combination:

Code: Select all

#stringTabForward = 98
#stringTabBackward = 99

Dim tabs(9, 1)

window = OpenWindow(#PB_Any, 0, 0, 590, 220, "Textbox Tab Order",
                    #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

StringGadget(1, 30, 30, 150, 30, "1. first tab")
StringGadget(2, 220, 30, 150, 30, "2. second tab")
StringGadget(3, 410, 30, 150, 30, "3. third tab")
StringGadget(4, 30, 90, 150, 30, "6. sixth tab ")
StringGadget(5, 220, 90, 150, 30, "5. fifth tab")
StringGadget(6, 410, 90, 150, 30, "4. fourth tab")
StringGadget(7, 30, 150, 150, 30, "7. seventh tab")
StringGadget(8, 220, 150, 150, 30, "8. eighth tab")
StringGadget(9, 410, 150, 150, 30, "9. ninth tab")
AddKeyboardShortcut(window ,#PB_Shortcut_Tab, #stringTabForward)
AddKeyboardShortcut(window ,#PB_Shortcut_Shift | #PB_Shortcut_Tab, #stringTabBackward)

tabs(1, 0) = 2 : tabs(1, 1) = 9   ; string 1 forwards to string 2 and reverses to string 9
tabs(2, 0) = 3 : tabs(2, 1) = 1   ; string 2 forwards to string 3 and reverses to string 1
tabs(3, 0) = 6 : tabs(3, 1) = 2   ; string 3 forwards to string 6 and reverses to string 2
tabs(4, 0) = 7 : tabs(4, 1) = 5   ; string 4 forwards to string 7 and reverses to string 5
tabs(5, 0) = 4 : tabs(5, 1) = 6   ; string 5 forwards to string 4 and reverses to string 6
tabs(6, 0) = 5 : tabs(6, 1) = 3   ; string 6 forwards to string 5 and reverses to string 3
tabs(7, 0) = 8 : tabs(7, 1) = 4   ; string 7 forwards to string 8 and reverses to string 4
tabs(8, 0) = 9 : tabs(8, 1) = 7   ; string 8 forwards to string 9 and reverses to string 7
tabs(9, 0) = 1 : tabs(9, 1) = 8   ; string 9 forwards to string 1 and reverses to string 8

SetActiveGadget(1)

Repeat
  event= WaitWindowEvent()
  Select event
    Case #PB_Event_CloseWindow
      appQuit = #True
    Case #PB_Event_Menu
      Select EventMenu()
        Case #stringTabForward          
          nextTab = tabs(GetActiveGadget(), 0)
          SetActiveGadget(nextTab)  
        Case #stringTabBackward          
          prevTab = tabs(GetActiveGadget(), 1)
          SetActiveGadget(prevTab)           
      EndSelect
  EndSelect
Until appQuit

Re: Positioning the cursor

Posted: Sun Sep 15, 2024 10:21 am
by boddhi
Combining the two @TI-944 examples ( :mrgreen: :wink: ) and considering the use of enumeration (as strongly recommended by the PB team) and/or gadget numbers lower than 65535:

Code: Select all

#stringTabForward = 98
#stringTabBackward = 99

window = OpenWindow(#PB_Any, 0, 0, 590, 220, "Textbox Tab Order",
                    #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

StringGadget(1, 30, 30, 150, 30, "1. first tab")
StringGadget(2, 220, 30, 150, 30, "2. second tab")
StringGadget(3, 410, 30, 150, 30, "3. third tab")
StringGadget(4, 30, 90, 150, 30, "6. sixth tab ")
StringGadget(5, 220, 90, 150, 30, "5. fifth tab")
StringGadget(6, 410, 90, 150, 30, "4. fourth tab")
StringGadget(7, 30, 150, 150, 30, "7. seventh tab")
StringGadget(8, 220, 150, 150, 30, "8. eighth tab")
StringGadget(9, 410, 150, 150, 30, "9. ninth tab")

;SetGadgetData(GadgetNr, Tab gadget << 16 + ShiftTab gadget)
SetGadgetData(1, 2 << 16 + 9)     ; string 1 forwards to string 2 and reverses to string 9
SetGadgetData(2, 3 << 16 + 1)     ; string 2 forwards to string 3 and reverses to string 1
SetGadgetData(3, 6 << 16 + 2)     ; string 3 forwards to string 6 and reverses to string 2
SetGadgetData(4, 7 << 16 + 5)     ; string 4 forwards to string 7 and reverses to string 5
SetGadgetData(5, 4 << 16 + 6)     ; string 5 forwards to string 4 and reverses to string 6
SetGadgetData(6, 5 << 16 + 3)     ; string 6 forwards to string 5 and reverses to string 3
SetGadgetData(7, 8 << 16 + 4)     ; string 7 forwards to string 8 and reverses to string 4
SetGadgetData(8, 9 << 16 + 7)     ; string 8 forwards to string 9 and reverses to string 7
SetGadgetData(9, 1 << 16 + 8)     ; string 9 forwards to string 1 and reverses to string 8

AddKeyboardShortcut(window ,#PB_Shortcut_Tab, #stringTabForward)
AddKeyboardShortcut(window ,#PB_Shortcut_Shift | #PB_Shortcut_Tab, #stringTabBackward)

SetActiveGadget(1)

Repeat
  event= WaitWindowEvent()
  Select event
    Case #PB_Event_CloseWindow
      appQuit = #True
    Case #PB_Event_Menu
      Select EventMenu()
        Case #stringTabForward          
          SetActiveGadget(GetGadgetData(GetActiveGadget()) >> 16)
        Case #stringTabBackward          
          DataGadget = GetGadgetData(GetActiveGadget())
          SetActiveGadget(DataGadget - (DataGadget >> 16) << 16)
          ; or in one line :
          ;SetActiveGadget(GetGadgetData(GetActiveGadget()) - (GetGadgetData(GetActiveGadget()) >> 16) << 16)
      EndSelect
  EndSelect
Until appQuit

Re: Positioning the cursor

Posted: Sun Sep 15, 2024 10:54 am
by Mindphazer
Nice example ! 8)

Re: Positioning the cursor

Posted: Mon Sep 16, 2024 12:30 am
by ClueLess
Hi

Thank You all

I got it working, Just design the gadjet one after the other.

The idea is to havave the cursor jumping from gadjet to the next gadjet when we press the tab key

Thak You

Re: Positioning the cursor

Posted: Mon Sep 16, 2024 6:38 am
by TI-994A
ClueLess wrote: Mon Sep 16, 2024 12:30 am... Just design the gadjet one after the other. ...

Only on Windows. macOS and Linux do not conform to the order of the gadget declarations.

Re: Positioning the cursor

Posted: Mon Sep 16, 2024 8:27 am
by mk-soft
All platforms

Link: viewtopic.php?p=441357#p441357