AutoIT执行应用程序热键

时间:2015-09-23 20:40:34

标签: autoit

我看到有很多关于使用autoit设置热键的指南。我想做的是执行应用程序热键。

例如,我有这个加载firefox

Run(@ProgramFilesDir & "\Mozilla Firefox\firefox.exe", "", @SW_MINIMIZE)
Opt("WinTitleMatchMode", 2)
WinWait("Mozilla Firefox")
WinSetState("Mozilla Firefox", "", @SW_MINIMIZE)

现在在firefox的菜单中,我可以看到Ctrl + D的组合会为页面添加书签。有没有办法在加载firefox后通过autoit执行此操作?

由于

3 个答案:

答案 0 :(得分:1)

只需使用“发送”命令即可。另请查看SendKeepActive。

答案 1 :(得分:1)

激活FireFox窗口后,只需使用发送命令。

Send("^d")

答案 2 :(得分:0)

有几种方法可以做到这一点。我将在下面列出几种不同的方法。

方法一 - 发送密钥

#import "AppDelegate.h"

@interface AppDelegate ()

@property (assign) CFMachPortRef myEventTap;
@property (assign) CFRunLoopSourceRef myRunLoopSource;

@end

@implementation AppDelegate

CGEventRef KeyHandler(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon)
{
    // don't modify keys on the numeric keypad
    CGEventFlags flags = CGEventGetFlags(event);
    if (flags & kCGEventFlagMaskNumericPad)
        return event;

    // get the typed character
    UniCharCount actualStringLength;
    UniChar chars[3];
    CGEventKeyboardGetUnicodeString(event, 3, &actualStringLength, chars);

    // uncomment this line to log the typed character, the modifier flags (Shift, Option, etc.) and the key code (number of the key on the keyboard)
    NSLog(@"%C %llX %lld", chars[0], flags, CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode));

    if (actualStringLength == 1) {
        // map the character from string1 to string2 and vice versa
        NSString *string1 = @"&é\"'(§è!çà";
        NSString *string2 = @"1234567890";
        NSString *typedString = [NSString stringWithCharacters:chars length:1];
        // find the index of the typed character in string1
        NSRange range = [string1 rangeOfString:typedString];
        if (range.location != NSNotFound)
            // get the character in string2 at the same index
            chars[0] = [string2 characterAtIndex:range.location];
        else {
            // find the index of the typed character in string2
            range = [string2 rangeOfString:typedString];
            if (range.location != NSNotFound)
                // get the character in string1 at the same index
                chars[0] = [string1 characterAtIndex:range.location];
        }
        // if the character was found, replace the character in the event
        if (range.location != NSNotFound)
            CGEventKeyboardSetUnicodeString(event, 1, chars);
    }
    return event;
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // create an event tap, we want the key down and key up events
    CGEventMask eventMask = CGEventMaskBit(kCGEventKeyDown) | CGEventMaskBit(kCGEventKeyUp);
    self.myEventTap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault, eventMask, KeyHandler, NULL);
    if (self.myEventTap) {
        // create a runloop source
        self.myRunLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, self.myEventTap, 0);
        // add it to the current run loop
        CFRunLoopAddSource(CFRunLoopGetCurrent(), self.myRunLoopSource, kCFRunLoopCommonModes);
    }
}

- (void)applicationWillTerminate:(NSNotification *)aNotification {
    // remove the event tap
    if (self.myRunLoopSource) {
        CFRunLoopSourceInvalidate(self.myRunLoopSource);
        CFRelease(self.myRunLoopSource);
    }
    if (self.myEventTap)
        CFRelease(self.myEventTap);
}

@end

方法二 - AutoIt HotKeys

; Below is simply the code you listed in the example to open Firefox and wait for it to load.
Run(@ProgramFilesDir & "\Mozilla Firefox\firefox.exe", "", @SW_MINIMIZE)
Opt("WinTitleMatchMode", 2)
WinWait("Mozilla Firefox")
WinSetState("Mozilla Firefox", "", @SW_MINIMIZE)

; Once FireFox is loaded, and you are at the page you want to bookmark, send Ctrl+D to the page to bookmark it. Since you started the browser Minimized, you will need to activate the page first.

; Activate the window
WinActivate("Mozilla Firefox")

; Send "Ctrl + D"
Send("^d")

方法三 - MouseMove(不推荐)

; Create a HotKey controller
HotKeySet("^d", "bookmarkPage")

; Your code is below again.
Run(@ProgramFilesDir & "\Mozilla Firefox\firefox.exe", "", @SW_MINIMIZE)
Opt("WinTitleMatchMode", 2)
WinWait("Mozilla Firefox")
WinSetState("Mozilla Firefox", "", @SW_MINIMIZE)


; The function that is called when "Ctrl + D" is pressed.

Func bookmarkPage ()
    ; Activate the window
    WinActivate("Mozilla Firefox")

    ; Send they keys
    Send("^d")
EndFunc

我强烈建议不要使用最后一个选项。我希望其中一个适合你!