Steveiwonder Posted January 11, 2010 Share Posted January 11, 2010 (edited) Hey I'm attempting to write a program to detect a sequence of specific keypresses to then run other scripts on my PC/functions etc. I'm trying to make my job easier for me. So if for example i press FBN (not all held down just in that sequence) i want to the to send the clip bored. There is also this... when i type "F" followed by a sequence of 6 numbers for example "F1698273" i would like it to run a script i have made to open a trouble ticket. I'm stuggling to work out how to monitor my keypresses then execute a function depending on what i've pressed. Make sense? I hope so. Apprciate any advice. Wheni get home i will post my script so far. thanks Steve Edited January 11, 2010 by Steveiwonder They call me MrRegExpMan Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted January 11, 2010 Moderators Share Posted January 11, 2010 (edited) Steveiwonder,You need to be careful asking to detect keypresses - it could be construed as wanting a KeyLogger, which is not welcome here. You can read why here.However, as you are only looking for some specific combinations, you can use StringRegExp like this:#include <GUIConstants.au3> $hGUI = GUICreate("Input data", 220, 90) $hInput = GUICtrlCreateInput ( "", 10, 20, 200, 20) $hButton_Test = GUICtrlCreateButton ("Test", 20, 50, 80, 30) $hButton_Exit = GUICtrlCreateButton("Exit", 120, 50, 80, 30) GuiSetState(@SW_SHOW) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE, $hButton_Exit Exit Case $hButton_Test If StringRegExp(GUICtrlRead($hInput), "(?i)\AF(BN|\d{6})") Then $sResult = StringUpper(GUICtrlRead($hInput)) MsgBox(0, "Match", $sResult & " is a valid input") EndIf GUICtrlSetData($hInput, "") EndSwitch WendAnd because I realise you will want to know, the "(?i)\AF(BN|\d{6})" translates as:(?i) - case insensitive matching \A - Begin at the start (i.e do not match in mid-string) F - match F (or f or course!) (..|..) - match either one side or the other of the |, so we are looking for either BN - which matches FBN, or \d{6} - F followed by exactly 6 digitsI hope that is clear enough. StringRegExp is very powerful, but not easy to grasp at first. I would class it as one of the hardest things I have ever had to learn in coding - but then maybe that is just me! M23Edit:Here is another way to do it which does not require pressing the button - it fires automatically if you enter a valid sequence of keys:#include <GUIConstants.au3> $hGUI = GUICreate("Input data", 220, 90) $hInput = GUICtrlCreateInput ( "", 10, 20, 200, 20) $hButton_Exit = GUICtrlCreateButton("Exit", 70, 50, 80, 30) GuiSetState(@SW_SHOW) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE, $hButton_Exit Exit EndSwitch If StringRegExp(GUICtrlRead($hInput), "(?i)\AF(BN|\d{6})") Then $sResult = StringUpper(GUICtrlRead($hInput)) MsgBox(0, "Match", $sResult & " is a valid input") GUICtrlSetData($hInput, "") EndIf Wend Edited January 11, 2010 by Melba23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
AdmiralAlkex Posted January 11, 2010 Share Posted January 11, 2010 @Steveiwonder Sounds to me like you want hotstrings, see the example for _WinAPI_SetWindowsHookEx in the helpfile .Some of my scripts: ShiftER, Codec-Control, Resolution switcher for HTC ShiftSome of my UDFs: SDL UDF, SetDefaultDllDirectories, Converting GDI+ Bitmap/Image to SDL Surface Link to comment Share on other sites More sharing options...
Steveiwonder Posted January 11, 2010 Author Share Posted January 11, 2010 Thanks both for your replies will take a look at the options avilable. Keylogging is definatly not my intention, just detecting a series of specific key presses. Thanks again. Steve They call me MrRegExpMan Link to comment Share on other sites More sharing options...
anixon Posted January 12, 2010 Share Posted January 12, 2010 Hey I'm attempting to write a program to detect a sequence of specific keypresses to then run other scripts on my PC/functions etc. I'm trying to make my job easier for me. So if for example i press FBN (not all held down just in that sequence) i want to the to send the clip bored. There is also this... when i type "F" followed by a sequence of 6 numbers for example "F1698273" i would like it to run a script i have made to open a trouble ticket. I'm stuggling to work out how to monitor my keypresses then execute a function depending on what i've pressed. Make sense? I hope so. Apprciate any advice. Wheni get home i will post my script so far. thanks Steve You may also find this useful Ant.. expandcollapse popup#Include <Misc.au3> While 1 _KeysMonitor() Sleep(20000) WEnd ;//Alt+Keys Monitor Func _KeysMonitor() $dll = DllOpen("user32.dll") ;//Alt+A sEmailSMS Administration If _IsPressed('12', $dll) And _IsPressed('41', $dll) Then DllClose($dll) ;//Your Function EndIf ;//Alt+C If _IsPressed('12', $dll) And _IsPressed('43', $dll) Then DllClose($dll) ;//Your Function EndIf ;//Alt+E If _IsPressed('12', $dll) And _IsPressed('45', $dll) Then DllClose($dll) ;//Your Function EndIf ;//Alt+M If _IsPressed('12', $dll) And _IsPressed('4D', $dll) Then DllClose($dll) ;//Your Function EndIf ;//Alt+R If _IsPressed('12', $dll) And _IsPressed('52', $dll) Then DllClose($dll) ;//Your Function EndIf ;//Alt+S If _IsPressed('12', $dll) And _IsPressed('53', $dll) Then DllClose($dll) ;//Your Function EndIf ;//Alt+V If _IsPressed('12', $dll) And _IsPressed('56', $dll) Then DllClose($dll) ;//Your Function EndIf ;//Alt + End to Close If _IsPressed('12', $dll) And _IsPressed('23', $dll) Then DllClose($dll) If MsgBox(262180, "Your Name", " EXITING ", 20) = 6 Then Exit EndIf EndIf DllClose($dll) EndFunc ;==>_KeysMonitor Link to comment Share on other sites More sharing options...
darkjohn20 Posted January 12, 2010 Share Posted January 12, 2010 I believe in the example for _WinAPI_SetWindowsHookEx there is a way to do this. Just replace those words with what you want. expandcollapse popup#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 #include <WinAPI.au3> #include <WindowsConstants.au3> #include <StructureConstants.au3> Opt('MustDeclareVars', 1) Global $hHook, $hStub_KeyProc, $buffer = "" _Main() Func _Main() Local $hmod $hStub_KeyProc = DllCallbackRegister("_KeyProc", "long", "int;wparam;lparam") $hmod = _WinAPI_GetModuleHandle(0) $hHook = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($hStub_KeyProc), $hmod) MsgBox(4096, "", "Click OK, then in notepad type..." & _ @LF & @LF & "Jon" & @LF & "AutoIt" & @LF & @LF & "Press Esc to exit script") Run("Notepad") WinWait("Untitled -") WinActivate("Untitled -") While 1 Sleep(10) WEnd EndFunc ;==>_Main Func EvaluateKey($keycode) If (($keycode > 64) And ($keycode < 91)) _ ; a - z Or (($keycode > 96) And ($keycode < 123)) _ ; A - Z Or (($keycode > 47) And ($keycode < 58)) Then ; 0 - 9 $buffer &= Chr($keycode) Switch $buffer Case "FBA" ToolTip("Do stuff") Case "F123151" ToolTip("Another thing") EndSwitch ElseIf ($keycode > 159) And ($keycode < 164) Then Return ElseIf ($keycode = 27) Then ; esc key Exit Else $buffer = "" EndIf EndFunc ;==>EvaluateKey ;=========================================================== ; callback function ;=========================================================== Func _KeyProc($nCode, $wParam, $lParam) Local $tKEYHOOKS $tKEYHOOKS = DllStructCreate($tagKBDLLHOOKSTRUCT, $lParam) If $nCode < 0 Then Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam) EndIf If $wParam = $WM_KEYDOWN Then EvaluateKey(DllStructGetData($tKEYHOOKS, "vkCode")) Else Local $flags = DllStructGetData($tKEYHOOKS, "flags") Switch $flags Case $LLKHF_ALTDOWN ConsoleWrite("$LLKHF_ALTDOWN" & @LF) Case $LLKHF_EXTENDED ConsoleWrite("$LLKHF_EXTENDED" & @LF) Case $LLKHF_INJECTED ConsoleWrite("$LLKHF_INJECTED" & @LF) Case $LLKHF_UP ConsoleWrite("$LLKHF_UP: scanCode - " & DllStructGetData($tKEYHOOKS, "scanCode") & @TAB & "vkCode - " & DllStructGetData($tKEYHOOKS, "vkCode") & @LF) EndSwitch EndIf Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam) EndFunc ;==>_KeyProc Func OnAutoItExit() _WinAPI_UnhookWindowsHookEx($hHook) DllCallbackFree($hStub_KeyProc) EndFunc ;==>OnAutoItExi Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now