-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement keyboard Enable/Disable functionality
- Loading branch information
Showing
6 changed files
with
180 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package keyboard | ||
|
||
type Controller struct { | ||
KeyboardLocked bool | ||
disableChan chan bool | ||
} | ||
|
||
type KeyboardActions interface { | ||
LockKeyboard() | ||
UnlockKeyboard() | ||
} | ||
|
||
func NewController() *Controller { | ||
return NewKeyboardController() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
//go:build darwin | ||
// +build darwin | ||
|
||
package keyboard | ||
|
||
/* | ||
#cgo CFLAGS: -x objective-c | ||
#cgo LDFLAGS: -framework Cocoa -framework Quartz | ||
#include <Cocoa/Cocoa.h> | ||
#include <Quartz/Quartz.h> | ||
static CFMachPortRef eventTap; | ||
static CFRunLoopSourceRef runLoopSource; | ||
CGEventRef myCGEventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) { | ||
return NULL; // Block all keyboard events | ||
} | ||
void disableKeyboard() { | ||
eventTap = CGEventTapCreate( | ||
kCGHIDEventTap, | ||
kCGHeadInsertEventTap, | ||
kCGEventTapOptionDefault, | ||
CGEventMaskBit(kCGEventKeyDown) | CGEventMaskBit(kCGEventFlagsChanged), | ||
myCGEventCallback, | ||
NULL | ||
); | ||
if (!eventTap) { | ||
fprintf(stderr, "Failed to create event tap\n"); | ||
exit(1); | ||
} | ||
runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0); | ||
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes); | ||
CGEventTapEnable(eventTap, true); | ||
CFRunLoopRun(); | ||
} | ||
void enableKeyboard() { | ||
if (eventTap) { | ||
CGEventTapEnable(eventTap, false); | ||
CFRunLoopRemoveSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes); | ||
CFRelease(runLoopSource); | ||
CFRelease(eventTap); | ||
} | ||
} | ||
*/ | ||
import "C" | ||
|
||
func NewKeyboardController() *Controller { | ||
return &Controller{ | ||
KeyboardLocked: false, | ||
disableChan: make(chan bool), | ||
} | ||
} | ||
|
||
func (c *Controller) LockKeyboard() { | ||
// If already locked, do nothing | ||
if c.KeyboardLocked { | ||
return | ||
} | ||
|
||
c.toggleLockUnlock() | ||
|
||
if c.disableChan == nil { | ||
c.disableChan = make(chan bool) | ||
} | ||
|
||
go func() { | ||
for { | ||
select { | ||
case <-c.disableChan: | ||
return | ||
default: | ||
C.disableKeyboard() | ||
} | ||
} | ||
}() | ||
} | ||
|
||
func (c *Controller) UnlockKeyboard() { | ||
// If already unlocked, do nothing | ||
if !c.KeyboardLocked { | ||
return | ||
} | ||
|
||
c.toggleLockUnlock() | ||
|
||
if c.disableChan != nil { | ||
close(c.disableChan) | ||
|
||
// Reset the channel to indicate it has been closed | ||
c.disableChan = nil | ||
} | ||
|
||
C.enableKeyboard() | ||
} | ||
|
||
func (c *Controller) toggleLockUnlock() { | ||
c.KeyboardLocked = !c.KeyboardLocked | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//go:build !darwin | ||
// +build !darwin | ||
|
||
package keyboard | ||
|
||
import "C" | ||
import "fmt" | ||
|
||
func NewKeyboardController() *Controller { | ||
return &Controller{} | ||
} | ||
|
||
func (c *Controller) LockKeyboard() { | ||
fmt.Println("LockKeyboard called on non-macOS platform. No operation performed.") | ||
} | ||
|
||
func (c *Controller) UnlockKeyboard() { | ||
fmt.Println("UnlockKeyboard called on non-macOS platform. No operation performed.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters