Jump to content

Set variable on keypress


Recommended Posts

Hey, I wanted to set a variable to a certain number based on the key pressed and then set the position of the mouse to a value inside of an array based on the key pressed, without creating a function for each variable. I will do functions if I absolutely need to, but here is what I have so far, and can't seem to find a

func print()

while true

Hotkeyset("^p", "keypress")

mousemove( 15, 65); test for proper function

mousemove( 75, 85);

$p = 14;

if HotKeySet("{NUMPAD0}") then $p = 0;

if HotKeySet("{NUMPAD1}") then $p = 1;

if HotKeySet("{NUMPAD2}") then $p = 2;

if HotKeySet("{NUMPAD3}") then $p = 3;

if HotKeySet("{NUMPAD4}") then $p = 4;

if HotKeySet("{NUMPAD5}") then $p = 5;

if HotKeySet("{NUMPAD6}") then $p = 6;

if HotKeySet("{NUMPAD7}") then $p = 7;

if HotKeySet("{NUMPAD8}") then $p = 8;

if HotKeySet("{NUMPAD9}") then $p = 9;

if HotKeySet("{NUMPADdiv}") then $p = 10;

if HotKeySet("{NUMPADmult}") then $p = 11;

if HotKeySet("{NUMPADsub}") then $p = 12;

while $p <> 14

readPos();

$xpos[$p] = $x;

iniWrite ( @ScriptDir & "\test.ini","Print",'xpos[' & $p & ']', $xpos[$p] );

$ypos[$p] = $y;

iniWrite ( @ScriptDir & "\test.ini","Print",'ypos[' & $p & ']', $ypos[$p] );

$p=14

wend

WEnd

EndFunc

I scoured google and the help file as best as I could, to no avail. Help would be much appreciated,as I would like to keep my program as smooth as possible.

Thank you.

Link to comment
Share on other sites

look at _ispressed

example:

#include <Misc.au3>

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

While 1
If _IsPressed("10", $hDLL) Then
ConsoleWrite("_IsPressed - Shift Key was pressed." & @CRLF)
; Wait until key is released.
While _IsPressed("10", $hDLL)
Sleep(250)
WEnd
ConsoleWrite("_IsPressed - Shift Key was released." & @CRLF)
ElseIf _IsPressed("1B", $hDLL) Then
MsgBox(0, "_IsPressed", "The Esc Key was pressed, therefore we will close the application.")
ExitLoop
EndIf
Sleep(250)
WEnd

DllClose($hDLL)
Edited by scullion
Link to comment
Share on other sites

_IsPressed may get delayed or may not be able to capture fast keystrokes

HotkeySet would be a better option

This will give you the basic structure of setting the hotkey

Global $p=-1
For $i=0 To 9
HotKeySet("{NUMPAD"&$i&"}",'_hotKeyFunc')
Next

HotKeySet("{NUMPADDIV}",'_hotKeyFunc')
HotKeySet("{NUMPADMULT}",'_hotKeyFunc')
HotKeySet("{NUMPADSUB}",'_hotKeyFunc')

Func _hotKeyFunc()
;ConsoleWrite('HotKeyPressed : '&@HotKeyPressed&@CRLF)
Local $sCount=StringRight(StringTrimRight(@HotKeyPressed,1),1)
Switch StringIsAlpha($sCount)
Case 1
Switch $sCount
Case 'V','v'
$p=10
Case 'T','t'
$p=11
Case 'B','b'
$p=12
Case Else
Return SetError(1,0,-1)
EndSwitch
Case 0
$p=$sCount
EndSwitch
Return 1
EndFunc
Local $sPrevious=-1
While $p<>12 ;Exit on Subtract pressed
;Sleep(250)
If $sPrevious<>$p Then
ConsoleWrite(' Value of P: '&$p&@CRLF)
$sPrevious=$p
EndIf
WEnd

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

Hey-- thank you for your helps-- but I'm still stuck. My script does not seem to recognize the key strokes.

Setting hotkeys works for the rest of my script, and even in this function; I'm leaning towards _isPressed to get this function going as I don't want to create a func _hotkeyfunc0, 1 2 3 4 5 6 7 8 9 10 11 12 as each would cost 3 lines of code to simply set a global variable,

Here is where I"m at

func print()
Local $hDLL = DllOpen("user32.dll"), $p=0
for $i=0 to 12 step 1
iniWrite ( @ScriptDir & "test.ini","Print",'xpos[' & $i & ']',0 )
iniWrite ( @ScriptDir & "test.ini","Print",'ypos[' & $i & ']', 0 )
Next
send("{space}");
Send("{i}");
while True
HotKeySet("{numpadenter}","keypress")
if _ispressed( "{numpad0}", $hDLL) then $p=0;
if _ispressed( "{numpad1}", $hDLL) then $p=1;
if _ispressed( "{numpad2}", $hDLL) then $p=2;
if _ispressed( "{numpad3}", $hDLL) then $p=3;
if _ispressed( "{numpad4}", $hDLL) then $p=4;
if _ispressed( "{numpad5}", $hDLL) then $p=5;
if _ispressed( "{numpad6}", $hDLL) then $p=6;
if _ispressed( "{numpad7}", $hDLL) then $p=7;
if _ispressed( "{numpad8}", $hDLL) then $p=8;
if _ispressed( "{numpad9}", $hDLL) then $p=9;
if _ispressed( "{numpaddiv}", $hDLL) then $p=10;
if _ispressed( "{numpadmult}", $hDLL) then $p=11;
if _ispressed( "{numpadsub}", $hDLL) then $p=12;
while $p<14
readPos();
;~ $xpos[$p] = $x
iniWrite ( @ScriptDir & "test.ini","Print",'xpos[' & $p & ']', $xpos[$p] )
;~ $ypos[$p] = $y
iniWrite ( @ScriptDir & "test.ini","Print",'ypos[' & $p & ']', $ypos[$p] )
$p = 14
mousemove(500,500);
WEnd
;~ mousemove(250,400);
wend
DllClose($hDLL)
EndFunc

it seems to clear out the array, and then set $xpos[0] and $ypos[0] the first time through-- then p gets set to 14, and the script goes inert.

Link to comment
Share on other sites

If you are going to start talking about variables, in your script and what happens

to them, then provide a code sample where people know what they are and where

they come from!

I'm looking at you $xpos and $ypos.

Take that advice and apply it to functions too (readpos())

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

I'm leaning towards _isPressed to get this function going as I don't want to create a func _hotkeyfunc0, 1 2 3 4 5 6 7 8 9 10 11 12 as each would cost 3 lines of code to simply set a global variable,

I guess u didnt see my Script, only one Function can carry out all the 12 variable assignments and is more efficient

Global $p=-1
For $i=0 To 9
HotKeySet("{NUMPAD"&$i&"}",'_hotKeyFunc')
Next

HotKeySet("{NUMPADDIV}",'_hotKeyFunc')
HotKeySet("{NUMPADMULT}",'_hotKeyFunc')
HotKeySet("{NUMPADSUB}",'_hotKeyFunc')

Func _hotKeyFunc()
;ConsoleWrite('HotKeyPressed : '&@HotKeyPressed&@CRLF)
Local $sCount=StringRight(StringTrimRight(@HotKeyPressed,1),1)
Switch StringIsAlpha($sCount)
Case 1
Switch $sCount
Case 'V','v'
$p=10
Case 'T','t'
$p=11
Case 'B','b'
$p=12
Case Else
Return SetError(1,0,-1)
EndSwitch
Case 0
$p=$sCount
EndSwitch
Return 1
EndFunc
Local $sPrevious=-1
While $p<>12 ;Exit on Subtract pressed
;Sleep(250)
If $sPrevious<>$p Then
ConsoleWrite(' Value of P: '&$p&@CRLF)
$sPrevious=$p
EndIf
WEnd

U can create a Single Func _hotKeyFunc to assign all the 12 values see the code above

Regards

Phoenix XL

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

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...