Jump to content

Error with _Ispressed?


Recommended Posts

I've been trying to get this thing to work for a long time now and I finally managed to get it working properly but now I'm experiencing a different problem that I never noticed (Not sure if it was already there).

When I use my arrow keys to move the box around it works fine but if I use my mouse (Like moving it) it makes the box go extremely fast. It is supposed to only add 1 pixel at a time so the box moves slower but when you move the mouse it acts like it moving 10+ pixels.

Here's the code and thanks for any helpĀ  :thumbsup:

#include<misc.au3>
#include<file.au3>
#include<guiconstants.au3>
$gui = GUICreate("Gui",400,400)
$input = GUICtrlCreateInput("",200,200,50,50)
$box = GUICtrlCreateInput("",200,20,50,50)
GUICtrlSetState($input,$gui_disable)
GUICtrlSetState($box,$gui_disable)
GUICtrlSetBkColor($box,0xFF0000)
GUICtrlSetBkColor($input,0x000000)
GUISetState()

while 1
   If _IsPressed("25") Then
      $cp = ControlGetPos($gui,"",$box)
      If down1() Then
      $cp2 = $cp[0] - 1
      GUICtrlSetPos($box,$cp2,$cp[1])
      EndIf
   EndIf
   If _IsPressed("26") Then
      $cp = ControlGetPos($gui,"",$box)
      If down2() Then
      $cp2 = $cp[1] - 1
      GUICtrlSetPos($box,$cp[0],$cp2)
      EndIf
   EndIf
   If _IsPressed("27") Then
      $cp = ControlGetPos($gui,"",$box)
      If down3() Then
      $cp2 = $cp[0] + 1
      GUICtrlSetPos($box,$cp2,$cp[1])
      EndIf
   EndIf
   If _IsPressed("28") Then
      $cp = ControlGetPos($gui,"",$box)
      If down4() Then
      $cp2 = $cp[1] + 1
      GUICtrlSetPos($box,$cp[0],$cp2)
      EndIf
      EndIf
   $msg = GUIGetMsg(1)
   Switch $msg[1]
   Case $gui
      Switch $msg[0]
      Case -3
         Exit
      EndSwitch
   EndSwitch
WEnd

         Func down1()
      $obj = ControlGetPos($gui,"",$input)
      $a = $cp[1] + $cp[3]
      $b = $cp[0] + $cp[2]
      $c = $cp[0]
      $d = $cp[1]
      If $a >= 200 And $b >= 200 And $c <= 251 And $d <= 250 Then
         Return False
      EndIf
      Return True
   EndFunc

         Func down2()
      $obj = ControlGetPos($gui,"",$input)
      $a = $cp[1] + $cp[3]
      $b = $cp[0] + $cp[2]
      $c = $cp[0]
      $d = $cp[1]
      If $a >= 250 And $b >= 200 And $c <= 250 And $d <= 251 Then
         Return False
      EndIf
      Return True
   EndFunc

      Func down3()
      $obj = ControlGetPos($gui,"",$input)
      $a = $cp[1] + $cp[3]
      $b = $cp[0] + $cp[2]
      $c = $cp[0]
      $d = $cp[1]
      If $a >= 200 And $b >= 199 And $c <= 250 And $d <= 250 Then
         Return False
      EndIf
      Return True
      EndFunc

      Func down4()
      $obj = ControlGetPos($gui,"",$input)
      $a = $cp[1] + $cp[3]
      $b = $cp[0] + $cp[2]
      $c = $cp[0]
      $d = $cp[1]
      If $a >= 199 And $b >= 200 And $c <= 250 And $d <= 250 Then
         Return False
      EndIf
      Return True
   EndFunc
Link to comment
Share on other sites

I guess it's due to GUIGetMsg, this last automatically idles the CPU when required.

So I'm afraid you can't do anything unless using the OnEvent mode instead.

Br, FireFox.

Edited by FireFox
Link to comment
Share on other sites

Yes, i think Firefox is right, when remove GuiGetMsg make it always fast. I put a sleep(100) to make it slow

#include<misc.au3>
#include<file.au3>
#include<guiconstants.au3>
$gui = GUICreate("Gui",400,400)
GUISetState(@SW_DISABLE,$gui)
$input = GUICtrlCreateInput("",200,200,50,50)
$box = GUICtrlCreateInput("",200,20,50,50)
GUICtrlSetState($input,$gui_disable)
GUICtrlSetState($box,$gui_disable)
GUICtrlSetBkColor($box,0xFF0000)
GUICtrlSetBkColor($input,0x000000)
GUISetState()

Do
   If _IsPressed("25") Then
      $cp = ControlGetPos($gui,"",$box)
      If down1() Then
      $cp2 = $cp[0] - 1
      GUICtrlSetPos($box,$cp2,$cp[1])
      Sleep(100)
      EndIf
   EndIf
   If _IsPressed("26") Then
      $cp = ControlGetPos($gui,"",$box)
      If down2() Then
      $cp2 = $cp[1] - 1
      GUICtrlSetPos($box,$cp[0],$cp2)
      Sleep(100)
      EndIf
   EndIf
   If _IsPressed("27") Then
      $cp = ControlGetPos($gui,"",$box)
      If down3() Then
      $cp2 = $cp[0] + 1
      GUICtrlSetPos($box,$cp2,$cp[1])
      Sleep(100)
      EndIf
   EndIf
   If _IsPressed("28") Then
      $cp = ControlGetPos($gui,"",$box)
      If down4() Then
      $cp2 = $cp[1] + 1
      GUICtrlSetPos($box,$cp[0],$cp2)
      Sleep(100)
      EndIf
      EndIf

Until 1=2


         Func down1()
      $obj = ControlGetPos($gui,"",$input)
      $a = $cp[1] + $cp[3]
      $b = $cp[0] + $cp[2]
      $c = $cp[0]
      $d = $cp[1]
      If $a >= 200 And $b >= 200 And $c <= 251 And $d <= 250 Then
         Return False
      EndIf
      Return True
   EndFunc

         Func down2()
      $obj = ControlGetPos($gui,"",$input)
      $a = $cp[1] + $cp[3]
      $b = $cp[0] + $cp[2]
      $c = $cp[0]
      $d = $cp[1]
      If $a >= 250 And $b >= 200 And $c <= 250 And $d <= 251 Then
         Return False
      EndIf
      Return True
   EndFunc

      Func down3()
      $obj = ControlGetPos($gui,"",$input)
      $a = $cp[1] + $cp[3]
      $b = $cp[0] + $cp[2]
      $c = $cp[0]
      $d = $cp[1]
      If $a >= 200 And $b >= 199 And $c <= 250 And $d <= 250 Then
         Return False
      EndIf
      Return True
      EndFunc

      Func down4()
      $obj = ControlGetPos($gui,"",$input)
      $a = $cp[1] + $cp[3]
      $b = $cp[0] + $cp[2]
      $c = $cp[0]
      $d = $cp[1]
      If $a >= 199 And $b >= 200 And $c <= 250 And $d <= 250 Then
         Return False
      EndIf
      Return True
   EndFunc
Edited by GordonFreeman
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...