Selecting an image in a grid using the keyboard Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Selecting an image in a grid using the keyboard

10 Apr 2021, 10:27

I have a grid containing pictures (for example, icons from a .dll file). The user can click an icon to select it. Here is a simplified example:

Take 1:

Code: Select all

Loop, 10
	Gui, Add, Picture, % (A_Index =1 ? "x10 y10" : "x+10 yp") . " w32 h32 gClick vIcon" . A_Index . " Icon" . A_Index, %A_WinDir%\System32\Shell32.dll
Gui, Show
return

Click:
MsgBox, %A_ThisLabel% on %A_GuiControl%.
return
The problem with this solution is that the user cannot use the keyboard to select an icon. The only working solution that I found to allow it is to add a Button control under each icon. In the buttons, I put the icon number. This is not as elegant as what I would want but this works, using Tab to choose an icon and Enter to select it.

Take 2:

Code: Select all

Loop, 10
	Gui, Add, Picture, % (A_Index =1 ? "x10 y10" : "x+10 yp") . " gClick w32 h32 vIcon" . A_Index . " Icon" . A_Index, %A_WinDir%\System32\Shell32.dll
Gui, Font, s6
Loop, 10
	Gui, Add, Button, % (A_Index =1 ? "x10 y40" : "x+10 yp") . " gEnter w32 h15 vButton" . A_Index, %A_Index% ; BackgroundTrans y20 x20 w20 h27 ReadOnly gSelected
Gui, Show
Return

Click:
Enter:
MsgBox, %A_ThisLabel% on %A_GuiControl%.
return
But ideally, I would not add a button. I would prefer to make the image itself selectable with the keyboard. I tried to add an empty Edit control over each icon and make it transparent and read-only. This allows to use the Tab key to choose an icon and, if I could find how, it would allow to press the Enter key to select it. Here is what I have done so far.

Take 3:

Code: Select all

Loop, 10
	Gui, Add, Picture, % (A_Index =1 ? "x10 y10" : "x+10 yp") . " gClick w32 h32 vIcon" . A_Index . " Icon" . A_Index, %A_WinDir%\System32\Shell32.dll
Gui, Font, s6
Loop, 10
	Gui, Add, Edit, % (A_Index =1 ? "x10 y10" : "x+10 yp") . " gEnter w32 h32 vEdit" . A_Index . "  -VScroll BackgroundTrans ReadOnly"
Gui, Show
Return

Click:
Enter:
MsgBox, %A_ThisLabel% on %A_GuiControl%.
return
But I have three issues with this code:

1) Making the edit control transparent did not work.
2) How could I make the Edit control react to the Enter keypress?
3) How could I suppress the blinking cursor in Edit controls?

Any suggestion about these issues? Or would you suggest another approach?

Thanks,
Jean
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey
User avatar
boiler
Posts: 16929
Joined: 21 Dec 2014, 02:44

Re: Selecting an image in a grid using the keyboard  Topic is solved

10 Apr 2021, 11:20

How about something like this where you use the left/right arrow keys and Tab and Shift+Tab to move the selector left or right, then press Enter or NumpadEnter to select the item?

Code: Select all

Loop, 10
	Gui, Add, Picture, % (A_Index =1 ? "x10 y10" : "x+10 yp") . " w32 h32 gClick vIcon" . A_Index . " Icon" . A_Index, %A_WinDir%\System32\Shell32.dll
Pos := 1
Gui, Add, Progress, x8 y8 w36 h1 BackgroundRed vP1
Gui, Add, Progress, x44 y8 w1 h36 BackgroundRed vP2
Gui, Add, Progress, x8 y44 w36 h1 BackgroundRed vP3
Gui, Add, Progress, x8 y8 w1 h36 BackgroundRed vP4
Gui, Show,, Icon Selector
return

Click:
MsgBox, %A_ThisLabel% on %A_GuiControl%.
return

#IfWinActive Icon Selector ahk_class AutoHotkeyGUI
Tab::
Right::
Pos := Pos = 10 ? 1 : Pos + 1
gosub, MoveSelector
return

+Tab::
Left::
Pos := Pos = 1 ? 10 : Pos - 1
gosub, MoveSelector
return

MoveSelector:
PosX := (Pos - 1) * 42 + 8
GuiControl, Move, P1, % "x" PosX " y8"
GuiControl, Move, P2, % "x" PosX + 36 " y8"
GuiControl, Move, P3, % "x" PosX " y44"
GuiControl, Move, P4, % "x" PosX " y8"
return

Enter::
NumpadEnter::
MsgBox, % "Selected icon " Pos
return

GuiClose:
ExitApp
User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: Selecting an image in a grid using the keyboard

10 Apr 2021, 11:28

boiler wrote:
10 Apr 2021, 11:20
How about something like this where you use the left/right arrow keys and Tab and Shift+Tab to move the selector left or right, then press Enter or NumpadEnter to select the item?
Yes! Using the Selector control is a brilliant idea. Thank you sooo much :-)
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Selecting an image in a grid using the keyboard

10 Apr 2021, 14:14

@boiler, funny trick! :)
Another one, with possibility selection by LButton:

Code: Select all

Gui, Main:New, +hwndhMain
Gui, Color, White
count := 10, Pics := [], selection := 1
Loop % count {
   n := mod(A_Index, count)
   Gui, New, +ParentMain +hwndhGui%n% -Caption
   Gui, Color, % A_Index = 1 ? 0xC4DDFC : "White"
   Gui, Add, Pic, hwndhPic%n% x5 y5 w32 h32 Icon%A_Index%, Shell32.dll
   k := A_ScreenDPI/96
   Gui, Show, % "NA x" . (42*(A_Index - 1) + 10)*k . " y" . 10*k . " w42 h42"
   Pics[hGui%n%] := n, Pics[hPic%n%] := n
}
Gui, Main:Show, % "w" 42*count + 20 . " h62"
Return

MainGuiClose:
   ExitApp

#If WinActive("ahk_id" . hMain)
Left::
Right::
   k := (A_ThisHotkey = "Right")*2 - 1
   Gui, % hGui%selection% ": Color", White
   selection := mod(selection + k + count, count)
   Gui, % hGui%selection% ": Color", 0xC4DDFC
   Return

~LButton::
   MouseGetPos,,,, hCtrl, 2
   if Pics.HasKey(hCtrl) {
      if (A_ThisHotkey = A_PriorHotkey && A_TimeSincePriorHotkey < 300)
         MsgBox, Item%selection%
      else {
         Gui, % hGui%selection% ": Color", White
         selection := Pics[hCtrl]
         Gui, % hGui%selection% ": Color", 0xC4DDFC
      }
   }
   Return

Enter::
   MsgBox, Item%selection%
   Return
Last edited by teadrinker on 10 Apr 2021, 15:22, edited 3 times in total.
User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: Selecting an image in a grid using the keyboard

10 Apr 2021, 14:35

teadrinker wrote:
10 Apr 2021, 14:14
Another one, with possibility selection by LButton:
Yes, another great solution. Tough choice !
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey
User avatar
boiler
Posts: 16929
Joined: 21 Dec 2014, 02:44

Re: Selecting an image in a grid using the keyboard

10 Apr 2021, 16:54

teadrinker wrote:
10 Apr 2021, 14:14
Another one, with possibility selection by LButton:
...
Nice code, as always.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Decar, doodles333 and 218 guests