Jump to content

Detect Certain Keys.


Recommended Posts

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 by Steveiwonder

They call me MrRegExpMan

Link to comment
Share on other sites

  • Moderators

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
Wend

And 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 digits

I 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! ;)

M23

Edit:

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 by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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

#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

I believe in the example for _WinAPI_SetWindowsHookEx there is a way to do this. Just replace those words with what you want.

#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

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