Jump to content

Adding Text and Numbers


 Share

Recommended Posts

I have a function which is going to handle the process of selecting my target application and press the key I tell it to press.

Its "KeySend" which is run like so:

KeySend("Key", "pressed")

Which tells it to WinActive() the target application (designated via a Global) and send the "Key" as a "pressed" key (As opposed to a held/multiple key press). The function also handles some other things which are not of importance; Nevertheless, I need to use it over the built in autoit CtrlSend() function.

My issue is this: My function sends numbers which are read as just that, numbers (i.e. KeySend(1, "pressed")). However, It processes other keys differently (i.e. Escape = "Esc" or "Escape", Space = "Space", etc.).

In my script, I have the KeySend function inside of a For/Next loop which I use the Var as the key in order to send a number to the Number Pad. It looks like so:

For $i = 0 to 4
  If $True Then
    KeySend("Num" & $i, "pressed")
  EndIf
Next

Unfortunately, this does not work. It never sends the full "NUM$i" to the KeySend Function. Basically, I need a way to append the number on the end of the text that is sent to my KeySend Function.

Currently, I have it going to an external function with a switch to return the full var, like so:

For $i = 0 to 4
    If $True Then
        $Ptr = GetKeyData($i)
        KeySend($Ptr, "pressed")
    EndIf
Next

Func GetKeyData($Num)
    Switch $Num
        Case 0
            Return "NUM0"
        Case 1
            Return "NUM1"
        Case 2
            Return "NUM2"
        Case 3
            Return "NUM3"
        Case 4
            Return "NUM4"
    EndSwitch
EndFunc

While this does in fact work, I feel as though it's a very rudimentary and inefficient way of doing this simple task. I have been having issues with my script sucking down too much CPU (About 40%-50%) and I'm trying to simplify everything. Also, in the future, I may need more than just the 5 numbers (0-4) and may need to do a bunch more (i.e. If $i = 200 Then Return "NUM2").

Any help is greatly appreciated!

Edited by ericnail
Link to comment
Share on other sites

  • Developers

Assuming you are using Send(), NUM1 is not a proper key for the send function. It should be {NUMPAD1}.

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

In my script, I have the KeySend function inside of a For/Next loop which I use the Var as the key in order to send a number to the Number Pad. It looks like so:

For $i = 0 to 4
  If $True Then
    KeySend("Num" & $i, "pressed")
  EndIf
Next

Unfortunately, this does not work.

There is nothing wrong with that. The problem is in your KeySend() function, not that loop:
$True = True

For $i = 0 to 4
  If $True Then
    KeySend("Num" & $i, "pressed")
  EndIf
Next

Func KeySend($sKey, $sAction)
    MsgBox(64, "KeySend()", "$sKey = " & $sKey & @CRLF & "$sAction = " & $sAction)
EndFunc

;)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Here is my KeySend Function:

Func KeySend($inkey, $evt = "pressed", $kdown = 2) ;System coded to send the actions to an inactive window
    ; send single keyboard event to non active window
    ; event = pressed, down, up
    ; kdown = key down delay
    ; note: supports only lower case keys + NUMx, Fx, some special keys and @
    $user32 = DllOpen("user32.dll")
    If $user32 = -1 Then
        ToolTip("User32 = -1!",0,0,"System Error", 1)
        return
    EndIf

    ; handling for special keys
    Switch StringUpper($inkey)
        Case "@"
            $skey = 0x40
            $lparam = 0x00100001
            DllCall($user32, 'int', "PostMessage", "hwnd", $hwnd, 'int', $WM_KEYDOWN, 'int', 0x71, "long", $lparam)
            DllCall($user32, 'int', "PostMessage", "hwnd", $hwnd, 'int', $WM_CHAR, 'int', $skey, "long", $lparam)
            Sleep(20)
            DllCall($user32, 'int', "PostMessage", "hwnd", $hwnd, 'int', $WM_KEYUP, 'int', 0x71, "long", BitOR($lparam, 0xC0000000))
        Case "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12"
            $skey = 0x6f + Int(StringMid($inkey, 2))
            ContinueCase
        Case "NUM0", "NUM1", "NUM2", "NUM3", "NUM4", "NUM5", "NUM6", "NUM7", "NUM8", "NUM9"
            If StringUpper(StringLeft($inkey, 3)) = "NUM" Then
                $skey = 0x60 + Int(StringMid($inkey, 4))
            EndIf
            ContinueCase
        Case "RETURN", "SPACE", "TAB", "BACK", "END", "HOME", "SNAPSHOT", "INSERT", "DELETE", "LEFT", "RIGHT", "UP", "DOWN", "ESC"
            Switch StringUpper($inkey)
                Case "SPACE"
                    $skey = 0x20
                Case "TAB"
                    $skey = 0x09
                Case "BACK"
                    $skey = 0x08
                Case "END"
                    $skey = 0x23
                Case "HOME"
                    $skey = 0x24
                Case "SNAPSHOT"
                    $skey = 0x2c
                Case "INSERT"
                    $skey = 0x2d
                Case "DELETE"
                    $skey = 0x2e
                Case "LEFT"
                    $skey = 0x25
                Case "RIGHT"
                    $skey = 0x27
                Case "UP"
                    $skey = 0x26
                Case "DOWN"
                    $skey = 0x28
                Case "ESC"
                    $skey = 0x1B
            EndSwitch
            $ret = DllCall($user32, 'int', "MapVirtualKey", 'int', $skey, 'int', 0)
            $lparam = BitShift($ret[0], -16)
            $lparam = BitOR($lparam, 1)
            DllCall($user32, 'int', "PostMessage", "hwnd", $hwnd, 'int', $WM_KEYDOWN, 'int', $skey, "long", $lparam)
            Sleep($kdown)
            DllCall($user32, 'int', "PostMessage", "hwnd", $hwnd, 'int', $WM_KEYUP, 'int', $skey, "long", BitOR($lparam, 0xC0000000))
        Case Else ; default lower case key handling
            $key = DllCall($user32, 'int', "VkKeyScan", 'int', Asc(StringLower($inkey)))
            $skey = $key[0]
            $ret = DllCall($user32, 'int', "MapVirtualKey", 'int', $skey, 'int', 0)
            $lparam = BitShift($ret[0], -16)
            $lparam = BitOR($lparam, 1)
            Select
                Case $evt = "pressed"
                    DllCall($user32, 'int', "PostMessage", "hwnd", $hwnd, 'int', $WM_KEYDOWN, 'int', $skey, "long", $lparam)
                    Sleep($kdown)
                    DllCall($user32, 'int', "PostMessage", "hwnd", $hwnd, 'int', $WM_KEYUP, 'int', $skey, "long", BitOR($lparam, 0xC0000000))
                Case $evt = "down"
                    DllCall($user32, 'int', "PostMessage", "hwnd", $hwnd, 'int', $WM_KEYDOWN, 'int', $skey, "long", $lparam)
                Case $evt = "up"
                    DllCall($user32, 'int', "PostMessage", "hwnd", $hwnd, 'int', $WM_KEYUP, 'int', $skey, "long", BitOR($lparam, 0xC0000000))
            EndSelect
    EndSwitch

    DllClose($user32)
EndFunc

I have tried it a million times; no-go ;)

Edited by ericnail
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...