-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from igorkulman/capslock-fix
Setting the initial Capslock state
- Loading branch information
Showing
4 changed files
with
70 additions
and
0 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,59 @@ | ||
// | ||
// Capslock.m | ||
// ThinkpadAssistant | ||
// | ||
// Created by Igor Kulman on 05/07/2020. | ||
// Copyright © 2020 Matthäus Szturc. All rights reserved. | ||
// | ||
|
||
#include <IOKit/IOKitLib.h> | ||
#include <IOKit/hidsystem/IOHIDLib.h> | ||
#include <IOKit/hidsystem/IOHIDParameter.h> | ||
#include <CoreFoundation/CoreFoundation.h> | ||
#include <stdbool.h> | ||
|
||
#include <libgen.h> | ||
|
||
/*! | ||
@brief Gets the current Capslock state | ||
@discussion This method can be used the gets the initial Capslock state whhen the application is first run. Needs to be done in Objective-C as Swift can only listen for modifier key presses only do determine Capslock being pressed. | ||
To use it, simply call getCapslockState() | ||
@return true if Capslock is enabled, false otherwise | ||
*/ | ||
bool getCapslockState() | ||
{ | ||
kern_return_t kr; | ||
io_service_t ios; | ||
io_connect_t ioc; | ||
CFMutableDictionaryRef mdict; | ||
bool state; | ||
|
||
mdict = IOServiceMatching(kIOHIDSystemClass); | ||
ios = IOServiceGetMatchingService(kIOMasterPortDefault, (CFDictionaryRef) mdict); | ||
if (!ios) | ||
{ | ||
if (mdict) | ||
CFRelease(mdict); | ||
return false; | ||
} | ||
|
||
kr = IOServiceOpen(ios, mach_task_self(), kIOHIDParamConnectType, &ioc); | ||
IOObjectRelease(ios); | ||
if (kr != KERN_SUCCESS) | ||
{ | ||
return false; | ||
} | ||
|
||
kr = IOHIDGetModifierLockState(ioc, kIOHIDCapsLockState, &state); | ||
if (kr != KERN_SUCCESS) | ||
{ | ||
IOServiceClose(ioc); | ||
return false; | ||
} | ||
|
||
IOServiceClose(ioc); | ||
return (bool)state; | ||
} |
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