If function key is in a string

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jameskhalil
Posts: 20
Joined: 07 Jun 2020, 13:56

If function key is in a string

07 Jun 2020, 14:47

Hi,

After every "Enter or NumpadEnter" : I need to run a set of lines of a script only if "F3" was NOT pressed before the last 7 caracters.

Unsuccessfully tried these : A_PriorKey, A_PriorHotkey
But I'm probably not using them well.

My script:

Code: Select all

Loop
	{
	Input, key, I L1 V
	log = %log%%key%
	}

~Enter::
	{
	keySequence := SubStr(log, -7)
	if (keySequence does not contain "F3")
		{
		...
		log =
		}
	return
	}

~NumpadEnter::
	{
	keySequence := SubStr(log, -7)
	if (keySequence does not contain "F3")
		{
		...
		log =
		}
	return
	}
Thanks
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: If function key is in a string

07 Jun 2020, 15:34

if !InStr(keySequence,"F3")
jameskhalil
Posts: 20
Joined: 07 Jun 2020, 13:56

Re: If function key is in a string

07 Jun 2020, 15:54

"F3" doesnt show in the string
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: If function key is in a string

07 Jun 2020, 16:11

if !InStr(keySequence,"F3") if NOTInStr(keySequence,"F3")

Code: Select all

key := "F3"
MsgBox %  InStr(key,"F3") ; 1/true
MsgBox % !InStr(key,"F3") ; 0/false
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: If function key is in a string

07 Jun 2020, 17:20

Maybe this will get you started:

Code: Select all

#NoEnv
#InstallKeybdHook
#KeyHistory 20    ; Sets the maximum number of keyboard and mouse events displayed by the KeyHistory window
SetBatchLines, -1

~Enter::
~NumpadEnter::
    if WasKeyBeforeLastSevenChars("F3")
		MsgBox F3 was pressed before the last 7 chars
	else
		MsgBox F3 was NOT pressed before the last 7 chars
return

WasKeyBeforeLastSevenChars(key)
{
	static skipKeys := "|Enter|NumpadEnter|BackSpace|Tab|Alt|Shift|Control|LButton|RButton|MButton|"    ; <<<--- Add more here as needed (non printing keys)
	pos1 := (InStr(txt := ScriptInfo("KeyHistory"), "------------------------------------------------------------------------------------------------") + 111)
	pos2 := InStr(txt, "Press [F5] to refresh.")
	txt := StrReplace(SubStr(txt, pos1, pos2-pos1), A_Space)
	Loop, Parse, txt, `n, `r
		Loop, Parse, A_LoopField, %A_Tab%
			switch A_Index
			{
			    case 3:
				       if (A_LoopField = "d")                         ; Discard: key down events
						   continue 2
				case 5:
				       if InStr(skipKeys, "|" . A_LoopField . "|")    ; Discard: non printing keys
					       continue 2
				       keys .= A_LoopField . "|"
			}
	keys := RTrim(keys,"|")
	lastKeysArr := StrSplit(keys, "|")
	Loop % lastKeysArr.length() - 7
	    if (lastKeysArr[A_Index] = key)
			return true
	return false
}

ScriptInfo(Command)
{   ; https://www.autohotkey.com/boards/viewtopic.php?t=9656
    static hEdit := 0, pfn, bkp
    if !hEdit {
        hEdit := DllCall("GetWindow", "ptr", A_ScriptHwnd, "uint", 5, "ptr")
        user32 := DllCall("GetModuleHandle", "str", "user32.dll", "ptr")
        pfn := [], bkp := []
        for i, fn in ["SetForegroundWindow", "ShowWindow"] {
            pfn[i] := DllCall("GetProcAddress", "ptr", user32, "astr", fn, "ptr")
            DllCall("VirtualProtect", "ptr", pfn[i], "ptr", 8, "uint", 0x40, "uint*", 0)
            bkp[i] := NumGet(pfn[i], 0, "int64")
        }
    }
 
    if (A_PtrSize=8) {  ; Disable SetForegroundWindow and ShowWindow.
        NumPut(0x0000C300000001B8, pfn[1], 0, "int64")  ; return TRUE
        NumPut(0x0000C300000001B8, pfn[2], 0, "int64")  ; return TRUE
    } else {
        NumPut(0x0004C200000001B8, pfn[1], 0, "int64")  ; return TRUE
        NumPut(0x0008C200000001B8, pfn[2], 0, "int64")  ; return TRUE
    }
 
    static cmds := {ListLines:65406, ListVars:65407, ListHotkeys:65408, KeyHistory:65409}
    cmds[Command] ? DllCall("SendMessage", "ptr", A_ScriptHwnd, "uint", 0x111, "ptr", cmds[Command], "ptr", 0) : 0
 
    NumPut(bkp[1], pfn[1], 0, "int64")  ; Enable SetForegroundWindow.
    NumPut(bkp[2], pfn[2], 0, "int64")  ; Enable ShowWindow.
 
    ControlGetText, text,, ahk_id %hEdit%
    return text
}

HTH
jameskhalil
Posts: 20
Joined: 07 Jun 2020, 13:56

Re: If function key is in a string

08 Jun 2020, 10:13

Error at lauch: image attached
Attachments
Sans titre.png
Sans titre.png (6.64 KiB) Viewed 476 times
jameskhalil
Posts: 20
Joined: 07 Jun 2020, 13:56

Re: If function key is in a string

15 Jun 2020, 07:42

The script now launches, Thank you. But It is more complex than my level (not able to play with it).

Here are my issues:
I like BoBo's simple script but I don't think it detects the F3, or I cant make it work.

Xtra's script: Is there a lighter/less complicated script than that (for begginers)?
The pressed F3 key is sometime not detected.
I can't find the variable with the last typed 7 caracters (i need to past them back after pressing F3). Is it lastKeysArr or txt? I tried MsgBox to see what they contain but its empty.
I have a barcode scanner that I have to avoid interfering with.

Thank you in advance.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 174 guests