Sign in to follow this  
Followers 0
BrianCJ

Using _IsPressed to copy a pattern in a grid

4 posts in this topic

Hello, I am currently working with some 4th graders using code.org to teach them the basics of programing.  The first of their lessons that we are doing is called "graph paper programing"  - check out the lesson at:  https://learn.code.org/unplugged/unplug3.pdf

You have a set of arrows that tell the student what direction to move / fill in the block in a 4x4 grid. I am trying to put together a few more challenging ones using a 10x10 grid.  Instead of manually trying to write down each direction, I am trying to have AutoIt transcribe the arrows (ill be switching it over to an arrow font later).  I have a 10x10 image drawn in Google Sheets and am trying to capture each of the arrow keys for the directional movements and the X when something needs to be shaded in.  I've been playing around with the following code but sometimes it misses a key press or it puts in double the key press. Any suggestions would be great, thanks!

#include <MsgBoxConstants.au3>
#include <File.au3>
#include <Misc.au3>

;Variables

Local $hDLL = DllOpen("user32.dll")

$file = FileOpen("10x10grid.txt", 1)
   ; Check if file opened for writing OK
   If $file = -1 Then
     MsgBox(0, "Error", "Unable to open file.")
     Exit
  EndIf

  While 1
   ; X key
    If _IsPressed("58", $hDLL) Then
       FileWrite ($file, "X, ")  
    EndIf

     ; left arrow
    If _IsPressed("25", $hDLL) Then
       FileWrite ($file, "B ")
    EndIf

   ; up arrow
    If _IsPressed("26", $hDLL) Then
       FileWrite ($file, "C ")
    EndIf

   ; right arrow
    If _IsPressed("27", $hDLL) Then
       FileWrite ($file, "A ")
    EndIf

   ; down arrow
    If _IsPressed("28", $hDLL) Then
       FileWrite ($file, "D ")
    EndIf

    If _IsPressed("1B", $hDLL) Then
        MsgBox($MB_SYSTEMMODAL, "_IsPressed", "The Esc Key was pressed, therefore we will close the application.")
        ExitLoop
    EndIf
    Sleep(50)
WEnd

DllClose($hDLL)

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

forgive-me... my english is poor...
if I understand, you want something like this

In my first concept, I was used _IsPressed too... but this is not cool... the code is very very very fast to move (to control and move the target).

if you work with childrens, maybe fast is not good...

Than I work with HotKeyPress, is better, is more controllable.

#include-once
#include <Misc.au3>
#include <Array.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>

HotKeySet("{END}", "_quit")
HotKeySet("{UP}", "_move_up")
HotKeySet("{DOWN}", "_move_down")
HotKeySet("{LEFT}", "_move_left")
HotKeySet("{RIGHT}", "_move_right")
HotKeySet("{SPACE}", "_paint") ; to change color
OnAutoItExitRegister("before_exit")

;$iMode = 0    => moviment limited on borders
;$iMode = 1    => moviment free
Global $iMode = 1 ; try 0 or 1

; list of colors avaiables
Global $aInk[] = [0xFFFFFF, 0x000000] ; put more hexadecimal colors here

; start position of cursor
Global $aPos[2] = [0, 0]

; $aSize[0] => width of grid
; $aSize[1] => height of grid
; $aSize[2] => size of tile (widht and height is same)
Global $aSize[3] = [4, 4, 32] ; change the grid size
;Global $aSize[3] = [8, 8, 40] ; change the grid size

Global $aButton[$aSize[0]][$aSize[1]]
Global $aColor[$aSize[0]][$aSize[1]]

Global $hGui = GUICreate("", ($aSize[0] + 1) * $aSize[2], ($aSize[1] + 1) * $aSize[2], -1, -1, Default, $WS_EX_COMPOSITED)
GUISetFont(20, 400, 0, "Times New Roman", $hGui)
build()
GUISetState(@SW_SHOW, $hGui)

While Sleep(20)
WEnd

Func _quit()
    Exit
EndFunc   ;==>_quit

Func before_exit()
EndFunc   ;==>before_exit

Func build()
    Local $pos = Int($aSize[2] / 2)
    For $ii = 0 To $aSize[0] - 1
        For $jj = 0 To $aSize[1] - 1
            $aButton[$ii][$jj] = GUICtrlCreateLabel("", $pos + $aSize[2] * $ii, $pos + $aSize[2] * $jj, $aSize[2], $aSize[2], BitOR($SS_CENTER, $SS_SUNKEN))
            GUICtrlSetColor($aButton[$ii][$jj], 0x00FF00)
            GUICtrlSetBkColor($aButton[$ii][$jj], $aInk[0])
            $aColor[$ii][$jj] = 0
            If $ii == $aPos[0] And $jj = $aPos[1] Then _mark(True)
        Next
    Next
EndFunc   ;==>build

Func _move_up()
    _mark()
    $aPos[1] -= 1
    If $aPos[1] < 0 Then $aPos[1] = $iMode ? $aSize[1] - 1 : 0
    _mark(True)
EndFunc   ;==>_move_up

Func _move_down()
    _mark()
    $aPos[1] += 1
    If $aPos[1] > $aSize[1] - 1 Then $aPos[1] = $iMode ? 0 : $aSize[1] - 1
    _mark(True)
EndFunc   ;==>_move_down

Func _move_left()
    _mark()
    $aPos[0] -= 1
    If $aPos[0] < 0 Then $aPos[0] = $iMode ? $aSize[0] - 1 : 0
    _mark(True)
EndFunc   ;==>_move_left

Func _move_right()
    _mark()
    $aPos[0] += 1
    If $aPos[0] > $aSize[0] - 1 Then $aPos[0] = $iMode ? 0 : $aSize[0] - 1
    _mark(True)
EndFunc   ;==>_move_right

Func _paint()
    $aColor[$aPos[0]][$aPos[1]] += 1
    If $aColor[$aPos[0]][$aPos[1]] > UBound($aInk, 1) - 1 Then $aColor[$aPos[0]][$aPos[1]] = 0
    GUICtrlSetBkColor($aButton[$aPos[0]][$aPos[1]], $aInk[$aColor[$aPos[0]][$aPos[1]]])
EndFunc   ;==>_paint

Func _mark($input = False)
    GUICtrlSetData($aButton[$aPos[0]][$aPos[1]], $input ? "x" : "")
EndFunc   ;==>_mark
Edited by Detefon

m(o.O)m

Share this post


Link to post
Share on other sites

BrianCJ,

As shown in the example code in the Help file, you need to check for key release to prevent _IsPressed being actioned multiple times every key press:

#include <MsgBoxConstants.au3>
#include <Misc.au3>

;Variables

Local $hDLL = DllOpen("user32.dll")


While 1
    ; X key
    If _IsPressed("58", $hDLL) Then
        ConsoleWrite("X" & @CRLF)
        While _IsPressed("58", $hDLL)
            Sleep(10)
        WEnd
    EndIf

    ; left arrow
    If _IsPressed("25", $hDLL) Then
        ConsoleWrite("B" & @CRLF)
        While _IsPressed("25", $hDLL)
            Sleep(10)
        WEnd
    EndIf

    ; up arrow
    If _IsPressed("26", $hDLL) Then
        ConsoleWrite("C" & @CRLF)
        While _IsPressed("26", $hDLL)
            Sleep(10)
        WEnd
    EndIf

    ; right arrow
    If _IsPressed("27", $hDLL) Then
        ConsoleWrite("A" & @CRLF)
        While _IsPressed("27", $hDLL)
            Sleep(10)
        WEnd
    EndIf

    ; down arrow
    If _IsPressed("28", $hDLL) Then
        ConsoleWrite("D" & @CRLF)
        While _IsPressed("28", $hDLL)
            Sleep(10)
        WEnd
    EndIf

    If _IsPressed("1B", $hDLL) Then
        MsgBox($MB_SYSTEMMODAL, "_IsPressed", "The Esc Key was pressed, therefore we will close the application.")
        ExitLoop
    EndIf
    Sleep(50)
WEnd

DllClose($hDLL)
That works perfectly for me. :)

And if you are looking to introduce some more advanced concepts later on you might like to think of loops and arrays: ;)

#include <MsgBoxConstants.au3>
#include <Misc.au3>

;Variables

Local $hDLL = DllOpen("user32.dll")

Local $aKeys[6] = ["58", "25", "26", "27", "28", "1B"]

While 1

    For $i = 0 To UBound($aKeys) - 1
        If _IsPressed($aKeys[$i], $hDLL) Then
            Switch $aKeys[$i]
                Case "58"
                    ConsoleWrite("X" & @CRLF)
                Case "25"
                    ConsoleWrite("B" & @CRLF)
                Case "26"
                    ConsoleWrite("C" & @CRLF)
                Case "27"
                    ConsoleWrite("A" & @CRLF)
                Case "28"
                    ConsoleWrite("D" & @CRLF)
                Case "1B"
                    MsgBox($MB_SYSTEMMODAL, "_IsPressed", "The Esc Key was pressed, therefore we will close the application.")
                    ExitLoop 2
            EndSwitch
            While _IsPressed($aKeys[$i], $hDLL)
                Sleep(10)
            WEnd
        EndIf
    Next
    Sleep(10)

WEnd

DllClose($hDLL)
M23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Great thanks for the help!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0