Jump to content

Key sequence function trigger


Recommended Posts

Hi I'm trying to have a script running in the background that executes a certain code when a specific key sequence is entered. Basically instead of using HotKeySet I thought it would be less intrusive if a function was called with this method. So lets say, for instance, I'd like my personal Firefox profile to launch instead of the shared profile, so I hit the "Insert" key followed by the "1" key.

So naturally I have to use HotKeySet("{INSERT}", "ItBegins") and have the ItBegins function to do the rest. At first I thought I could do this with the _IsPressed() function within a loop, but there's a problem: I want the function to exit the loop whenever ANY other key or input besides {1} is entered, so if say the Insert key is pressed by accident I don't have to worry about firefox popping up when I type 1 for whatever other reason. Also I might use other sequences beside that one like Insert->2 and so forth.

Any solutions besides bloating the code with every possible key stroke? :huh2:

Here's what I have

#include <GuiToolbar.au3>
#include <Misc.au3>

$dll = DllOpen("user32.dll")
;Opt ("TrayIconHide",1) Disabled while I get this to work


HotKeySet("{INSERT}", "Engage")

While 1
    Sleep(100)
WEnd


Func Engage()
    While 1
        Sleep(100)
        If _IsPressed("31", $dll) Then
            MsgBox(0,"","This is where firefox is launched")
            ExitLoop
        ElseIf _IsPressed("32", $dll) Then
            MsgBox(0,"","This is a completely unrelated code made by 1337 H4xX0Rz")
            ExitLoop
        ElseIf (_IsPressed("31", $dll) = 0) And (_IsPressed("32", $dll) = 0) And (_IsPressed(*, $dll) = 1) Then ;The asterisk is just so you know I'm trying to detect every possible input.
            MsgBox(0,"","Ignoring input")
            ExitLoop
        EndIf
    WEnd
EndFunc

Another thing that bugs me is that the numbers still get typed in.

Is there any way to sort of block them when Insert is pressed?

Thanks!

Link to comment
Share on other sites

Il go ahead and see if i can find a way to stop the loop if any key is pressed

Edit:

IDEA!

Use _IsPressed()!

And lets say insert is 23 then write:

If Not _IsPressed(23) then...

Firstly, i dont think that would work.

Secondly, insert can't be used with _IsPressed

Edited by Maffe811

[font="helvetica, arial, sans-serif"]Hobby graphics artist, using gimp.Automating pc stuff, using AutoIt.Listening to music, using Grooveshark.[/font]Scripts:[spoiler]Simple ScreenshotSaves you alot of trouble when taking a screenshot!Don't remember what happened with this, but aperantly the exe is all i got.If you don't want to run it, simply don't._IsRun UDFIt figures out if the script has ben ran before based on the info in a ini file.If you don't want to use exactly what i wrote, you can use it as inspiration.[/spoiler]

Link to comment
Share on other sites

Il go ahead and see if i can find a way to stop the loop if any key is pressed

Edit:

IDEA!

Use _IsPressed()!

And lets say insert is 23 then write:

If Not _IsPressed(23) then...

Firstly, i dont think that would work.

Secondly, insert can't be used with _IsPressed

Darn!

I'm looking at the UDF, maybe I can tweak it a bit...

Link to comment
Share on other sites

i found a way to make _IsPressed work, i just have to tweak the code so it will kill whatevers running on anykey pressed

[font="helvetica, arial, sans-serif"]Hobby graphics artist, using gimp.Automating pc stuff, using AutoIt.Listening to music, using Grooveshark.[/font]Scripts:[spoiler]Simple ScreenshotSaves you alot of trouble when taking a screenshot!Don't remember what happened with this, but aperantly the exe is all i got.If you don't want to run it, simply don't._IsRun UDFIt figures out if the script has ben ran before based on the info in a ini file.If you don't want to use exactly what i wrote, you can use it as inspiration.[/spoiler]

Link to comment
Share on other sites

This^ is what my script is based on.

#include <misc.au3>

Func _IsAnyKeyPressed($start = 0, $end = 255)
    For $i = $start to $end
        if (_IsPressed(Hex($i))) Then return 1
    Next
    return 0
EndFunc

While 1
    Local $val
    $val = _IsAnyKeyPressed()
    If $val = 1 Then
        MsgBox(0,"NO!!", "Exiting loop!")
        Exitloop
    EndIf

    Sleep(1000)
    Send ("a")
WEnd

The problem is that it sleeps, sends and checks for anykey in a hundred of a sec!

So you have to hold the key to make it stop!

And thats not good at all!

Im trying to make it so it waits for the $val to be 1 and running the script...

And im not really sure how to do that!

[font="helvetica, arial, sans-serif"]Hobby graphics artist, using gimp.Automating pc stuff, using AutoIt.Listening to music, using Grooveshark.[/font]Scripts:[spoiler]Simple ScreenshotSaves you alot of trouble when taking a screenshot!Don't remember what happened with this, but aperantly the exe is all i got.If you don't want to run it, simply don't._IsRun UDFIt figures out if the script has ben ran before based on the info in a ini file.If you don't want to use exactly what i wrote, you can use it as inspiration.[/spoiler]

Link to comment
Share on other sites

IDEA!

Use _IsPressed()!

And lets say insert is 23 then write:

If Not _IsPressed(23) then...

Firstly, i dont think that would work.

Secondly, insert can't be used with _IsPressed

Just realised i missed insert in the list:

2D INS key

I searched for insert, while i shoulda searched for ins... :huh2:

So the above might work, but it wont help if the loop problem is fixed...

[font="helvetica, arial, sans-serif"]Hobby graphics artist, using gimp.Automating pc stuff, using AutoIt.Listening to music, using Grooveshark.[/font]Scripts:[spoiler]Simple ScreenshotSaves you alot of trouble when taking a screenshot!Don't remember what happened with this, but aperantly the exe is all i got.If you don't want to run it, simply don't._IsRun UDFIt figures out if the script has ben ran before based on the info in a ini file.If you don't want to use exactly what i wrote, you can use it as inspiration.[/spoiler]

Link to comment
Share on other sites

why dont you use a combination of keys instead the sequence...

HotKeySet("^+{INSERT}", "Engage") ;Ctrl + Shift + insert
HotKeySet("!+{INSERT}", "unrelated") ; Alt + Shift + insert
HotKeySet("^{DEL}", "_exit") ; Ctrl + del

While 1
    Sleep(100)
WEnd

Func Engage()
    MsgBox(0, "", "This is where firefox is launched")
EndFunc   ;==>Engage
Func unrelated()
    MsgBox(0, "", "This is a completely unrelated code made by 1337 H4xX0Rz")
EndFunc   ;==>unrelated
Func _exit()
    MsgBox(0, "", "This is a exit combination")
    Exit
EndFunc   ;==>exit

what you want is possible but i think is more complicated... you should use the _ispress api function to get the key that is pressed and then do a check if the key is one of what you want or not

EDIT : you solve it! :huh2: nice work maffe811

Edited by monoscout999
Link to comment
Share on other sites

Maffe811 you are a genius man! Dumb me was lost with arrays and other unnecessary stuff.

Here's what we finally get:

#include <GuiToolbar.au3>
#include <Misc.au3>

Func _IsAnyKeyPressed($start = 0, $end = 255)
    For $i = $start to $end
        if $i <> 45 Then ;because leaving insert pressed for too long can trigger the exitloop
            if (_IsPressed(Hex($i))) Then return 1
        EndIf

    Next
    return 0
EndFunc

$dll = DllOpen("user32.dll")
;Opt ("TrayIconHide",1)


HotKeySet("{INSERT}", "Engage")

While 1
    Sleep(8)
WEnd


Func Engage()
    While 1
        Sleep(8) ;really low value for quick strokes to be noticed, but not so low as to choke on CPU usage
        Local $val
        $val = _IsAnyKeyPressed()
        If $val = 1 Then
            If _IsPressed("31", $dll) Then
                MsgBox(0,"","This is where firefox is launched")
                $val = 0
                ExitLoop
            ElseIf _IsPressed("32", $dll) Then
                MsgBox(0,"","This is a completely unrelated code about 1337 H4xX0Rz")
                $val = 0
                ExitLoop
            Else
                $val = 0
                MsgBox(0,"NO!!", "Exiting loop!")
                Exitloop
            EndIf
        EndIf

    WEnd
EndFunc

Like a charm. Though CPU still shoots up when the Engage function runs, despite the SLeep, but it doesn't matter since it shouldn't have to wait for long. Thanks a lot!

I tried hotstrings too but ("{INS} {1}") doesn't seem to count as a string. I'd use this one for everything else I guess? :huh2:

Link to comment
Share on other sites

Maffe811 you are a genius man!

I don't know if i would go that far :huh2:

But now i just have to convince my girlfriend im a genius ;)

[font="helvetica, arial, sans-serif"]Hobby graphics artist, using gimp.Automating pc stuff, using AutoIt.Listening to music, using Grooveshark.[/font]Scripts:[spoiler]Simple ScreenshotSaves you alot of trouble when taking a screenshot!Don't remember what happened with this, but aperantly the exe is all i got.If you don't want to run it, simply don't._IsRun UDFIt figures out if the script has ben ran before based on the info in a ini file.If you don't want to use exactly what i wrote, you can use it as inspiration.[/spoiler]

Link to comment
Share on other sites

How will that help convincing her im a genius ? :huh2:

[font="helvetica, arial, sans-serif"]Hobby graphics artist, using gimp.Automating pc stuff, using AutoIt.Listening to music, using Grooveshark.[/font]Scripts:[spoiler]Simple ScreenshotSaves you alot of trouble when taking a screenshot!Don't remember what happened with this, but aperantly the exe is all i got.If you don't want to run it, simply don't._IsRun UDFIt figures out if the script has ben ran before based on the info in a ini file.If you don't want to use exactly what i wrote, you can use it as inspiration.[/spoiler]

Link to comment
Share on other sites

Well, she doesnt say no to alcohol :huh2:

[font="helvetica, arial, sans-serif"]Hobby graphics artist, using gimp.Automating pc stuff, using AutoIt.Listening to music, using Grooveshark.[/font]Scripts:[spoiler]Simple ScreenshotSaves you alot of trouble when taking a screenshot!Don't remember what happened with this, but aperantly the exe is all i got.If you don't want to run it, simply don't._IsRun UDFIt figures out if the script has ben ran before based on the info in a ini file.If you don't want to use exactly what i wrote, you can use it as inspiration.[/spoiler]

Link to comment
Share on other sites

_isanykeypressed() command is not recognised by my SciTe editor and it is also not there in my AutoIt help file...please explain the reason..

:huh2:

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
Link to comment
Share on other sites

_isanykeypressed() command is not recognised by my SciTe editor and it is also not there in my AutoIt help file...please explain the reason..

:huh2:

Func _IsAnyKeyPressed($start = 0, $end = 255)
    For $i = $start to $end
        if $i <> 45 Then ;because leaving insert pressed for too long can trigger the exitloop
            if (_IsPressed(Hex($i))) Then return 1
        EndIf

    Next
    return 0
EndFunc

Cause it is defined right there ^

[font="helvetica, arial, sans-serif"]Hobby graphics artist, using gimp.Automating pc stuff, using AutoIt.Listening to music, using Grooveshark.[/font]Scripts:[spoiler]Simple ScreenshotSaves you alot of trouble when taking a screenshot!Don't remember what happened with this, but aperantly the exe is all i got.If you don't want to run it, simply don't._IsRun UDFIt figures out if the script has ben ran before based on the info in a ini file.If you don't want to use exactly what i wrote, you can use it as inspiration.[/spoiler]

Link to comment
Share on other sites

oops....I was confused and mistook it as a command... :huh2:

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
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...