Jump to content

Hotkey function then send the same hotkey


Recommended Posts

Hey, I'd like to do an action with the button DEL then send DEL

This what I started with

Local $sString = StringReplace(_NowDate(), "/", "-")

Func _Start()
    _ScreenCapture_Capture(@DesktopDir & "\" & StringRegExpReplace(_Now(), '[/ :]', '-') & ".jpg")
    Send("{DEL}")
EndFunc   ;==>_Start

But it end up in a loop

So I looked online and I found a function with _IsPressed, it doesn't end up in a loop which is great, but the function isn't working.

HotKeySet("1", "one")

Func one()
    HotKeySet("1")
    $dll = DllOpen("user32.dll")
    While _IsPressed(31)
        _ScreenCapture_Capture(@DesktopDir & "\" & StringRegExpReplace(_Now(), '[/ :]', '-') & ".jpg")
        Send("1")
        Sleep(1)
        If Not _IsPressed(31) Then ExitLoop
    WEnd
    DllClose($dll)
    HotKeySet("1", "One")
EndFunc   ;==>one

Button 1 is registering normally but the screen capture function isn't working.

_WinAPI_Keybd_Event(0x2E, 0)
Sleep(8)
_WinAPI_Keybd_Event(0x2E, 2)

Interestingly, swapping "1" with  "{DEL}" doesn't even send the button DEL. I also tried the del button with win api, the del button still isn't being sent.

 

I am really confused. I just want to take a screenshot with date and time every time I press delete.

Link to post
Share on other sites

This worked for me:

#include <Date.au3>
#include <ScreenCapture.au3>

HotKeySet("1", "one")
HotKeySet("{ESC}","leave")

While 1
    Sleep(50)
WEnd

Func one()
    _ScreenCapture_Capture(@DesktopDir & "\" & StringRegExpReplace(_Now(), '[/ :]', '-') & ".jpg")
    HotKeySet("1")
    Send("1")
    Sleep(200)
    HotKeySet("1", "one")
EndFunc

Func leave()
    Exit
EndFunc

 

Link to post
Share on other sites

This can't be the entire script obviously, cus this would just run and exit. Not sure why you're using DllOpen without using the open handle in anything, perhaps you misunderstand what that function is for?

Anyway, I wrote this up:

#include <Date.au3>
#include <Misc.au3>
#include <ScreenCapture.au3>

Global $hUser32 = DllOpen('user32.dll')
OnAutoItExitRegister(_Exit)

While 1
    If _IsPressed('2E', $hUser32) Then
        _ScreenCapture_Capture(@DesktopDir & "\" & StringRegExpReplace(_Now(), '[/ :]', '-') & ".jpg")
        Sleep(100)
    EndIf
WEnd

Func _Exit()
    DllClose($hUser32)
EndFunc

 

Link to post
Share on other sites
3 minutes ago, therks said:

This can't be the entire script obviously, cus this would just run and exit. Not sure why you're using DllOpen without using the open handle in anything, perhaps you misunderstand what that function is for?

Anyway, I wrote this up:

#include <Date.au3>
#include <Misc.au3>
#include <ScreenCapture.au3>

Global $hUser32 = DllOpen('user32.dll')
OnAutoItExitRegister(_Exit)

While 1
    If _IsPressed('2E', $hUser32) Then
        _ScreenCapture_Capture(@DesktopDir & "\" & StringRegExpReplace(_Now(), '[/ :]', '-') & ".jpg")
        Sleep(100)
    EndIf
WEnd

Func _Exit()
    DllClose($hUser32)
EndFunc

 

This also worked perfectly. Thank you guys so much!

Link to post
Share on other sites
48 minutes ago, therks said:

This can't be the entire script obviously, cus this would just run and exit. Not sure why you're using DllOpen without using the open handle in anything, perhaps you misunderstand what that function is for?

Anyway, I wrote this up:

#include <Date.au3>
#include <Misc.au3>
#include <ScreenCapture.au3>

Global $hUser32 = DllOpen('user32.dll')
OnAutoItExitRegister(_Exit)

While 1
    If _IsPressed('2E', $hUser32) Then
        _ScreenCapture_Capture(@DesktopDir & "\" & StringRegExpReplace(_Now(), '[/ :]', '-') & ".jpg")
        Sleep(100)
    EndIf
WEnd

Func _Exit()
    DllClose($hUser32)
EndFunc

 

Hey, unfortunately, I wasn't paying too much attention :D

I want to capture the screen first and THEN send the DEL key.

Link to post
Share on other sites
1 hour ago, Davidowicza said:

This worked for me:

#include <Date.au3>
#include <ScreenCapture.au3>

HotKeySet("1", "one")
HotKeySet("{ESC}","leave")

While 1
    Sleep(50)
WEnd

Func one()
    _ScreenCapture_Capture(@DesktopDir & "\" & StringRegExpReplace(_Now(), '[/ :]', '-') & ".jpg")
    HotKeySet("1")
    Send("1")
    Sleep(200)
    HotKeySet("1", "one")
EndFunc

Func leave()
    Exit
EndFunc

 

I swapped the "1" with DEL to fit my case, and while it worked on my computer, testing it on another computer sends two deletes instead of one. Any idea why ?

Link to post
Share on other sites

Thank you @Davidowicza
I didn't realise that you sent a similar solution with Send("1")
Throwing the unset should always be the very 1st instruction in the function (and resetting it should be the last one) to avoid several calls to the HotkeySet function.

I like to compare this precaution to a similar one when we use _IsPressed :

If _IsPressed("01", $hDll) Then
    While _IsPressed("01", $hDll)
        Sleep(10)
    Wend
    ...

Reviewing some old scripts, I realise that sometimes I used both ways in the same HotkeySet function. I don't remember why I did it like that in the script below,  maybe because the unsetting wasn't the 1st instruction :

HotKeySet("{NUMPAD8}", "_8_Tab_Prec")
...

Func _8_Tab_Prec()
    If WinActive(" - Opera") Then
        HotKeySet("{NUMPAD8}")                  ; désactiver Hotkey 8
        While _IsPressed("68", $hDll)           ; 68 = touche 8 du clavier numérique
            Sleep(10)
        Wend
      
        ...
      
        HotKeySet("{NUMPAD8}", "_8_Tab_Prec")   ; réactiver Hotkey 8
    EndIf
EndFunc     ; ==> _8_Tab_Prec

 

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...