code to turn caps lock off and log out/off of MAC OSX

Mac OSX specific forum
purvis
New User
New User
Posts: 3
Joined: Tue Oct 06, 2015 11:10 pm

code to turn caps lock off and log out/off of MAC OSX

Post by purvis »

Hi
This is my first post on the forums and have yet to write my first useful code.
I have purebasic installed on mac osx snow leopard.

I always like to write small utilities when learning a new compiler.

I have issues logging into mac osx from a vnc client when the caps lock is on.
The vnc client software will not turn off the caps lock at the mac osx login screen and all the passwords we are using are in lowercase.
Our computers are rarely booted, the computers are usually logged in by some user and then logged out/off only.
I do a lot of technical support and usually by remote control software with a windows computer.

What I feel will help and be a good first program is to have an application on the apple desktop where the user can log off from.
I found some code the forums but not thing that exactly fits.
The code main code that I would be needing help to is
1. check the state of the caps lock and turn it off if is on.
2. run code to some how to log out.

I will have to be running a gui so that i can ask the user if that is what they really want to do.
I really do not like the mac os x way of logging off anyways, because there should be a way for user to abort a logoff.
I do think I can write the code to confirm that the user wants to logoff.
But the caps lock state is where i am having trouble with first.
I am not sure the best way to logoff as i have not researched that yet, either by some shell command or direct api.

Thank you for any help or pointers.
I look forward to contributing code to the forum and being a good member.
Paul
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: code to turn caps lock off and log out/off of MAC OSX

Post by wilbert »

Checking caps lock is easy but unfortunately I haven't been able to change its state.
It might be possible with the IOKit framework but I'm not familiar enough with that.

Code: Select all

ImportC ""
  CGEventSourceKeyState (stateID, key)
EndImport

If CGEventSourceKeyState(1, 57) & 1
  Debug "Caps lock is on"
Else
  Debug "Caps lock if off"
EndIf
Windows (x64)
Raspberry Pi OS (Arm64)
purvis
New User
New User
Posts: 3
Joined: Tue Oct 06, 2015 11:10 pm

Re: code to turn caps lock off and log out/off of MAC OSX

Post by purvis »

Thanks Wilbert.
knowing the state will at least get me half way there.
If I have notify the user to turn off caps lock, that will get me a lot of yardage because their should be a user in using this program.
I will test your code in a few hours.

Thanks for responding Wilbert.
I saw where somebody gave you high remarks in a post back on some scanner device code.

I will continue to,search for an automated solution to change the state.
But conditioning a user to check the caps lock state upon a logout/restart/shutdown may have advantages too.
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: code to turn caps lock off and log out/off of MAC OSX

Post by Keya »

for send key i found a stackexchange post that says "You can use the CGEvent APIs, such as CGEventCreateKeyboardEvent followed by CGEventPos", which looks inline with wilberts check key demo that uses CGEventSourceKeyState :)

but for specifically getting and setting the state of Caps Lock it seems there's also another approach using IOHIDSetModifierLockState/IOHIDGetModifierLockState:
https://discussions.apple.com/thread/70 ... 0&tstart=0
http://superuser.com/questions/285103/i ... ed-on-os-x
hmm it seems for this method you need to call IOServiceOpen first though... open a service just to check keyboard state? hopefully CGEventCreateKeyboardEvent is all thats needed heehee :)
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: code to turn caps lock off and log out/off of MAC OSX

Post by wilbert »

Keya wrote:hmm it seems for this method you need to call IOServiceOpen first though... open a service just to check keyboard state? hopefully CGEventCreateKeyboardEvent is all thats needed heehee :)
I tried posting a CGEventCreateKeyboardEvent created event but couldn't get it working :(
The pages you linked with functions from the IOKit framework is an approach I also noticed but seems very complicated to me.
Maybe Shardik is more familiar with that approach :?
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: code to turn caps lock off and log out/off of MAC OSX

Post by Keya »

it seems Shardik is the only person to have posted code using CGEventCreateKeyboardEvent ! a bit more complicated than i thought it might be though heehee :)
http://www.purebasic.fr/english/viewtop ... 0&p=374286

There's another post here - http://stackoverflow.topusers.net/quest ... nt-in-os-x
where the poor guy has written all the code for it but its still not working and nobody provided an answer

btw it seems the IOHID routines might only change the LED, and not the state itself! not 100% sure though
jack
Addict
Addict
Posts: 1336
Joined: Fri Apr 25, 2003 11:10 pm

Re: code to turn caps lock off and log out/off of MAC OSX

Post by jack »

Keya
that second link looks malicious to me, kept getting popup's about installing java plugin and something else
purvis
New User
New User
Posts: 3
Joined: Tue Oct 06, 2015 11:10 pm

Re: code to turn caps lock off and log out/off of MAC OSX

Post by purvis »

Thank you for your help.
I will have to return to this tomorrow.

For what it is worth.
I am looking at shelling out to the os and running a command "osascript" but having issues with it right now.
But this does not work either but i feel I am getting warm.

Code: Select all

/usr/bin/osascript -e 'tell application "System Events" to key code 39 using {command down}'
Post Reply