Skip to content

Commit

Permalink
Create a CloseWindow function
Browse files Browse the repository at this point in the history
This is a cleaner implementation of #5,
will be removed if it starts interfering with anticheat software
  • Loading branch information
HikariKnight committed Nov 16, 2021
1 parent 2e15d0c commit 3d871da
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 2 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/HikariKnight/hotkeyD

go 1.17
go 1.16

require (
github.com/MakeNowJust/hotkey v0.0.0-20200628032113-41fa0caa507a
Expand All @@ -11,6 +11,7 @@ require (

require (
github.com/lxn/win v0.0.0-20210218163916-a377121e959e // indirect
github.com/micmonay/keybd_event v1.1.1 // indirect
github.com/stretchr/testify v1.7.0 // indirect
golang.org/x/sys v0.0.0-20210309074719-68d13333faf2 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/lxn/win v0.0.0-20210218163916-a377121e959e h1:H+t6A/QJMbhCSEH5rAuRxh+
github.com/lxn/win v0.0.0-20210218163916-a377121e959e/go.mod h1:KxxjdtRkfNoYDCUP5ryK7XJJNTnpC8atvtmTheChOtk=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-sqlite3 v1.14.4/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI=
github.com/micmonay/keybd_event v1.1.1 h1:rv7omwXWYL9Lgf3PUq6uBgJI2k1yGkL/GD6dxc6nmSs=
github.com/micmonay/keybd_event v1.1.1/go.mod h1:CGMWMDNgsfPljzrAWoybUOSKafQPZpv+rLigt2LzNGI=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
28 changes: 27 additions & 1 deletion src/app/hkdaemon/createhotkeys_win.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func CreateHotkeys() {

// Make our hotkey
hkey.Register(intKey, intHotKey, func() {
fmt.Println("Hotkey Pressed!")
fmt.Println("Pause hotkey Pressed!")

// Get the absolute path to the executable
filename, _ := osext.Executable()
Expand All @@ -83,6 +83,32 @@ func CreateHotkeys() {
os.Exit(0)
})

case "CloseWindow":
// If the section is named CloseWindow
// Check if --pause is passed as an argument
if !strings.Contains(strings.Join(os.Args[1:], " "), "--pause") {
// If we are not in paused mode
// Check if the hotkey options are not undefined
if modKeys != "" && hotKey != "" {
// Make an empty intkey
var intKey = hotkey.None

// Convert the modkeys to a hotkey.Modifier
intKey = hotkeyd.String2Mod(modKeys)

// Get the hotkey from settings and convert to uint32
var intHotKey uint32 = hotkeyd.HotkeySwitch(hotKey)

// Make our hotkey
hkey.Register(intKey, intHotKey, func() {
fmt.Println("CloseWindow hotkey Pressed!")

//Send ALT+F4
hotkeyd.CloseWindow()
})
}
}

default:
// For any other hotkey definitions
// Check if --pause is passed as an argument
Expand Down
27 changes: 27 additions & 0 deletions src/app/hotkeyd/closewindow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//go:build windows
// +build windows

package hotkeyd

import (
"github.com/micmonay/keybd_event"
"time"
)

func CloseWindow() {
kb, err := keybd_event.NewKeyBonding()
if err != nil {
panic(err)
}

// Select keys to be pressed
kb.SetKeys(keybd_event.VK_F4)

// Set ALT to be pressed
kb.HasALT(true)

time.Sleep(10 * time.Millisecond)
kb.Press()
time.Sleep(10 * time.Millisecond)
kb.Release()
}
7 changes: 7 additions & 0 deletions src/hotkeys.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
Modkeys=ctrl+alt
Hotkey=q

# HotkeyD has a built in function to close the active window
# However due to limitations in the Windows API the win key cannot be used alone as a modkey!
# The entry for this hotkey has to be named [CloseWindow]
[CloseWindow]
Modkeys=alt
Hotkey=q

# Example "open notepad" hotkey
[notepad]
Modkeys=ctrl+alt
Expand Down

0 comments on commit 3d871da

Please sign in to comment.