CTRL + LeftClick

Windows specific forum
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

CTRL + LeftClick

Post by IdeasVacuum »

Been on a very good run for the past few days but now I have hit a problem. It seems so simple, the solution must be right under my nose but I can't see it. Change of coffee brand made no difference at all.

All I want to do is this: When the User clicks a button, a value is set in a TextGadget (1). If the CTRL key is being held down at the time, a different value is set (100). Fail Snippet:

Code: Select all

Enumeration
#Win
#Btn
#Txt
EndEnumeration


Procedure Win()
;-------------

     If OpenWindow(#Win,0,0,150,150,"Win",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)

                 TextGadget(#Txt,  50, 20, 50, 25, "0", #PB_Text_Center|#PB_Text_Border)
               ButtonGadget(#Btn,  50, 70, 50, 50, "Btn")
        AddKeyboardShortcut(#Win, #PB_Shortcut_Control, #Btn)
     EndIf

EndProcedure

Win()

iVal = 1

Repeat
                 iEvent = WaitWindowEvent()
          Select iEvent

                       Case #PB_Event_Menu: If(EventMenu() = #Btn) : iVal = 100 : EndIf

                       Case #PB_Event_Gadget

                            If(EventGadget() = #Btn)

                                     SetGadgetText(#Txt,Str(iVal))
                                     iVal = 1
                            EndIf
          EndSelect

Until iEvent = #PB_Event_CloseWindow

End
Edit: Really want it to work with CTRL. If the shortcut is tied to any 'regular' key, say 'Q', the above snippet works - Well, works-ish. Q + Button Click does insert the value '100'. Yet the next Button Click, withput holding Q down, should insert the value '1', but on the first click it does not - second click it does....

Edit2: This works, at the top of the loop (without PB shortcut):

Code: Select all

If GetAsyncKeyState_(#VK_CONTROL | #Btn) : iVal = 100 : Else : iVal = 1 : EndIf
:)
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
BasicallyPure
Enthusiast
Enthusiast
Posts: 539
Joined: Thu Mar 24, 2011 12:40 am
Location: Iowa, USA

Re: CTRL + LeftClick

Post by BasicallyPure »

Code: Select all

Enumeration
#Win
#Btn
#Txt
EndEnumeration


Procedure Win()
;-------------

     If OpenWindow(#Win,0,0,150,150,"Win",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)

                 TextGadget(#Txt,  50, 20, 50, 25, "0", #PB_Text_Center|#PB_Text_Border)
               ButtonGadget(#Btn,  50, 70, 50, 50, "Btn")
     EndIf

EndProcedure

Win()

iVal = 1

Repeat
                 iEvent = WaitWindowEvent()
          Select iEvent

                       Case #WM_KEYDOWN
                          If EventwParam() = #VK_CONTROL
                             iVal = 100
                          EndIf
                          
                       Case #WM_KEYUP
                          If EventwParam() = #VK_CONTROL
                             iVal = 1
                          EndIf
                          
                       Case #PB_Event_Gadget

                            If(EventGadget() = #Btn)
                              SetGadgetText(#Txt,Str(iVal))
                              ;iVal = 1
                            EndIf
          EndSelect

Until iEvent = #PB_Event_CloseWindow

End
BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: CTRL + LeftClick

Post by netmaestro »

All I want to do is this: When the User clicks a button, a value is set in a TextGadget (1). If the CTRL key is being held down at the time, a different value is set (100).
Seems straightforward enough, wouldn't you just check for the state of the CTRL key at the time of the button press?

Code: Select all

Enumeration
  #Win
  #Btn
  #Txt
EndEnumeration


Procedure Win()
  
  If OpenWindow(#Win,0,0,150,150,"Win",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
    
    TextGadget(#Txt,  50, 20, 50, 25, "0", #PB_Text_Center|#PB_Text_Border)
    ButtonGadget(#Btn,  50, 70, 50, 50, "Btn")
 
  EndIf
  
EndProcedure

Win()

iVal = 1

Repeat
  iEvent = WaitWindowEvent()
  Select iEvent
      
    Case #PB_Event_Gadget
      
      If(EventGadget() = #Btn)
        If GetAsyncKeyState_(#VK_CONTROL)&32768
          ival=100
        Else
          ival=1
        EndIf
        SetGadgetText(#Txt,Str(iVal))
      EndIf
      
  EndSelect
  
Until iEvent = #PB_Event_CloseWindow

End
BERESHEIT
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: CTRL + LeftClick

Post by IdeasVacuum »

Thanks chaps - narrowed it down to:

Code: Select all

If GetAsyncKeyState_(#VK_CONTROL) : iVal = 100 : Else : iVal = 1 : EndIf
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: CTRL + LeftClick

Post by netmaestro »

You narrowed it down a bit too far. GetAsyncKeyState can return a true value without the key being currently down. Only the most significant bit tests whether the key is up or down at the time of the call. 0 is up, 1 is down. But the least significant bit can also be found set, indicating that the key has been pressed one or more times since the last call to GetAsyncKeyState. If you want a reliable way to know if the key is down now or not, you must mask everything out of the result except the most significant bit. Therefore, GetAsyncKeyState_(#VK_CONTROL) & 32768 is needed. True, GetAsyncKeyState_(#VK_CONTROL) by itself will likely work most of the time, but most of the time isn't optimal.
BERESHEIT
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: CTRL + LeftClick

Post by IdeasVacuum »

Thanks netmaestro, as always - good advice, well explained.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply