You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Toggle caps lock programmatically

I'm looking for a solution, using C/C++, to toggle the caps lock status in my Mac mini. I wrote a Cocoa library, after referring the HID LED test tool code. It is able to toggle the LED status alone. The actual Caps lock status remains unchanged.


I tried using the X11 library function XChangeKeyboardControl() and XkbLockModifiers(() to set the lock, status. But the lock status change works only in the X11 terminal. The OS seems to be unaware of this change.


Please share your thoughts on this issue, and provide information about some Core Foundation or Cocoa API which I can use to achieve this.


Mac Mini System Software Overview

System Version : Mac OS X 10.6.8 (10K549)

Kernel Version : Darwin 10.8.0

Mac mini, Mac OS X (10.6.8), Kernel Version : Darwin 10.8.0

Posted on Jun 23, 2015 12:01 AM

Reply
Question marked as Top-ranking reply

Posted on Jun 24, 2015 12:43 AM

Hello


You might try something like the following code.


Briefly tested under OS X 10.6.8. Not sure it works well under recent OSes, for it uses old APIs.


Hope this may help,

H



/* file: main.c function: get and set capslock state complie: gcc -std=c99 -framework IOKit -framework CoreFoundation -o capslock main.c usage: ./capslock [0|1|-1] 0 : capslock off 1 : capslock on -1 : toggle capslock state : print current capslock state (0|1) * written and published as free software by Hiroto, 2015-06 */ #include <IOKit/IOKitLib.h> #include <IOKit/hidsystem/IOHIDLib.h> #include <IOKit/hidsystem/IOHIDParameter.h> #include <CoreFoundation/CoreFoundation.h> #include <libgen.h> // basename #define CAPSLOCK_OFF 0 #define CAPSLOCK_ON 1 #define CAPSLOCK_TOGGLE -1 #define CAPSLOCK_QUERY 9 int main(int argc, char ** argv) { kern_return_t kr; io_service_t ios; io_connect_t ioc; CFMutableDictionaryRef mdict; int op; bool state; if (argc < 2) op = CAPSLOCK_QUERY; else op = atoi(argv[1]); if (op != CAPSLOCK_ON && op != CAPSLOCK_OFF && op != CAPSLOCK_TOGGLE && op != CAPSLOCK_QUERY) { fprintf(stderr, "Usage: %s %s\n\t\%s\n\t%s\n\t%s\n\t%s\n", basename(argv[0]), "[0|1|-1]", " 0 : capslock off", " 1 : capslock on", "-1 : toggle capslock state", " : print current capslock state (0|1)" ); return 1; } mdict = IOServiceMatching(kIOHIDSystemClass); ios = IOServiceGetMatchingService(kIOMasterPortDefault, (CFDictionaryRef) mdict); if (!ios) { if (mdict) CFRelease(mdict); fprintf(stderr, "IOServiceGetMatchingService() failed: %x\n", kr); return (int) kr; } kr = IOServiceOpen(ios, mach_task_self(), kIOHIDParamConnectType, &ioc); IOObjectRelease(ios); if (kr != KERN_SUCCESS) { fprintf(stderr, "IOServiceOpen() failed: %x\n", kr); return (int) kr; } switch (op) { case CAPSLOCK_ON: case CAPSLOCK_OFF: state = (op == CAPSLOCK_ON); kr = IOHIDSetModifierLockState(ioc, kIOHIDCapsLockState, state); if (kr != KERN_SUCCESS) { IOServiceClose(ioc); fprintf(stderr, "IOHIDSetModifierLockState() failed: %x\n", kr); return (int) kr; } break; case CAPSLOCK_TOGGLE: kr = IOHIDGetModifierLockState(ioc, kIOHIDCapsLockState, &state); if (kr != KERN_SUCCESS) { IOServiceClose(ioc); fprintf(stderr, "IOHIDGetModifierLockState() failed: %x\n", kr); return (int) kr; } state = !state; kr = IOHIDSetModifierLockState(ioc, kIOHIDCapsLockState, state); if (kr != KERN_SUCCESS) { IOServiceClose(ioc); fprintf(stderr, "IOHIDSetModifierLockState() failed: %x\n", kr); return (int) kr; } break; case CAPSLOCK_QUERY: kr = IOHIDGetModifierLockState(ioc, kIOHIDCapsLockState, &state); if (kr != KERN_SUCCESS) { IOServiceClose(ioc); fprintf(stderr, "IOHIDGetModifierLockState() failed: %x\n", kr); return (int) kr; } break; } IOServiceClose(ioc); fprintf(stdout, "%d", (int) state); return 0; }

2 replies
Question marked as Top-ranking reply

Jun 24, 2015 12:43 AM in response to dipumn

Hello


You might try something like the following code.


Briefly tested under OS X 10.6.8. Not sure it works well under recent OSes, for it uses old APIs.


Hope this may help,

H



/* file: main.c function: get and set capslock state complie: gcc -std=c99 -framework IOKit -framework CoreFoundation -o capslock main.c usage: ./capslock [0|1|-1] 0 : capslock off 1 : capslock on -1 : toggle capslock state : print current capslock state (0|1) * written and published as free software by Hiroto, 2015-06 */ #include <IOKit/IOKitLib.h> #include <IOKit/hidsystem/IOHIDLib.h> #include <IOKit/hidsystem/IOHIDParameter.h> #include <CoreFoundation/CoreFoundation.h> #include <libgen.h> // basename #define CAPSLOCK_OFF 0 #define CAPSLOCK_ON 1 #define CAPSLOCK_TOGGLE -1 #define CAPSLOCK_QUERY 9 int main(int argc, char ** argv) { kern_return_t kr; io_service_t ios; io_connect_t ioc; CFMutableDictionaryRef mdict; int op; bool state; if (argc < 2) op = CAPSLOCK_QUERY; else op = atoi(argv[1]); if (op != CAPSLOCK_ON && op != CAPSLOCK_OFF && op != CAPSLOCK_TOGGLE && op != CAPSLOCK_QUERY) { fprintf(stderr, "Usage: %s %s\n\t\%s\n\t%s\n\t%s\n\t%s\n", basename(argv[0]), "[0|1|-1]", " 0 : capslock off", " 1 : capslock on", "-1 : toggle capslock state", " : print current capslock state (0|1)" ); return 1; } mdict = IOServiceMatching(kIOHIDSystemClass); ios = IOServiceGetMatchingService(kIOMasterPortDefault, (CFDictionaryRef) mdict); if (!ios) { if (mdict) CFRelease(mdict); fprintf(stderr, "IOServiceGetMatchingService() failed: %x\n", kr); return (int) kr; } kr = IOServiceOpen(ios, mach_task_self(), kIOHIDParamConnectType, &ioc); IOObjectRelease(ios); if (kr != KERN_SUCCESS) { fprintf(stderr, "IOServiceOpen() failed: %x\n", kr); return (int) kr; } switch (op) { case CAPSLOCK_ON: case CAPSLOCK_OFF: state = (op == CAPSLOCK_ON); kr = IOHIDSetModifierLockState(ioc, kIOHIDCapsLockState, state); if (kr != KERN_SUCCESS) { IOServiceClose(ioc); fprintf(stderr, "IOHIDSetModifierLockState() failed: %x\n", kr); return (int) kr; } break; case CAPSLOCK_TOGGLE: kr = IOHIDGetModifierLockState(ioc, kIOHIDCapsLockState, &state); if (kr != KERN_SUCCESS) { IOServiceClose(ioc); fprintf(stderr, "IOHIDGetModifierLockState() failed: %x\n", kr); return (int) kr; } state = !state; kr = IOHIDSetModifierLockState(ioc, kIOHIDCapsLockState, state); if (kr != KERN_SUCCESS) { IOServiceClose(ioc); fprintf(stderr, "IOHIDSetModifierLockState() failed: %x\n", kr); return (int) kr; } break; case CAPSLOCK_QUERY: kr = IOHIDGetModifierLockState(ioc, kIOHIDCapsLockState, &state); if (kr != KERN_SUCCESS) { IOServiceClose(ioc); fprintf(stderr, "IOHIDGetModifierLockState() failed: %x\n", kr); return (int) kr; } break; } IOServiceClose(ioc); fprintf(stdout, "%d", (int) state); return 0; }

Toggle caps lock programmatically

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.