LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-11-2015, 04:41 PM   #1
Lurkr
LQ Newbie
 
Registered: Jun 2015
Posts: 4

Rep: Reputation: Disabled
Problems writing script tied to a hotkey (Linux Mint 17, KDE) for Corsair K70 kbd


I've dug around everywhere and haven't found anything about this, so hopefully I'm not just missing a post and creating duplicates.

I recently bought a Corsair k70 mechanical keyboard, which unfortunately doesn't play well with linux when it comes to the indicator lights, so I decided to write a script to control them automatically. The result is the script below:

Code:
#!/bin/sh
#Still not quite right
x-terminal-emulator -e xset -led 1
if xset -q | grep -q "Caps Lock:   on"; then
x-terminal-emulator -e xset led 1
fi

if xset -q | grep -q "Caps Lock:   off"; then
x-terminal-emulator -e xset -led 1
fi
This is stored in 'CapsLockV2.sh', and I've got the permissions set for it to be runnable.

The issue I'm trying to work around is the fact with this code, anytime I hit capslock (the key I've bound it to), I get the terminal windows produced by x-terminal-emulator popping in and out of existence. I've tried running it without x-terminal-emulator, with the code like this:

Code:
#!/bin/sh
#Still not quite right
xset -led 1
if xset -q | grep -q "Caps Lock:   on"; then
xset led 1
fi

if xset -q | grep -q "Caps Lock:   off"; then
xset -led 1
fi
without any success. Aside from this admittedly minor annoyance, the code seems to work just fine for manually controlling the indicator lights. My experience with writing shell scripts dates from about 3:00PM yesterday, so it's probably something really silly I'm missing, buta s it stands, I'm not quite sure what's wrong--any help would be greatly appreciated!

Last edited by Lurkr; 06-12-2015 at 04:23 PM. Reason: making it more readable!
 
Old 06-12-2015, 11:18 AM   #2
rigor
Member
 
Registered: Sep 2011
Posts: 212

Rep: Reputation: Disabled
Hi Lurkr,


To set the caps lock led, for example, have you tried something like:

Code:
setleds -v -caps < /dev/console
 
Old 06-12-2015, 01:30 PM   #3
Lurkr
LQ Newbie
 
Registered: Jun 2015
Posts: 4

Original Poster
Rep: Reputation: Disabled
No joy, unfortunately--here's the output

Code:
# setleds -v -caps < /dev/console
Old flags:            NumLock off   CapsLock off   ScrollLock off
New flags:            NumLock off   CapsLock off   ScrollLock off
That's the result no matter the state of the keys (not that ScrollLock is being recognized anyway).

Just so it's on here and people can see my (admittedly horrible) 'solution', here's the issue to which I'm referring.

Last edited by Lurkr; 06-12-2015 at 04:22 PM.
 
Old 06-12-2015, 03:56 PM   #4
rigor
Member
 
Registered: Sep 2011
Posts: 212

Rep: Reputation: Disabled
Lurkr,

Sorry, I suppose I didn't word my statement very well!

This sets the caps lock LED off:
Code:
setleds -v -caps < /dev/console
whereas this sets the caps lock LED on:
Code:
setleds -v +caps < /dev/console
 
Old 06-12-2015, 04:22 PM   #5
Lurkr
LQ Newbie
 
Registered: Jun 2015
Posts: 4

Original Poster
Rep: Reputation: Disabled
rigor,

Oh! Thank you for specifying, I totally misunderstood! It seems to have issues controlling the LEDs in conjunction with my use of xset, which I suppose shouldn't be surprising, so it can't turn off the LED is xset has turned it on.

Substituting that into my script (using the same format as specified above, without x-terminal-emulator), I've got
Code:
#!/bin/sh
setleds -v -num < /dev/console
if (setleds -v < /dev/console |grep "Current flags:"|grep "NumLock on"); then
setleds -v +num < /dev/console
fi

if (setleds -v < /dev/console | grep "Current flags:"| grep "NumLock off"); then
setleds -v -num < /dev/console
fi
If nothing else, if I'm interpreting this correctly, when I turn on the LED via setleds, even if nothing else happens, this should turn off the numlock LED, which it's not doing; unfortunately, I'm not sure why.
 
Old 06-13-2015, 01:34 PM   #6
rigor
Member
 
Registered: Sep 2011
Posts: 212

Rep: Reputation: Disabled
Lurkr,

I may have missed details you provided, but I didn't think you had mentioned what you were using to cause your script to run. Without all the details, I can only offer some general comments.

Since I don't know what you're familiar with and what you aren't, I'll just try to cover the various factors that may affect the different ways in which you have tried to achieve your goal:

1) For me, if I try to use setleds with /dev/console as a "standard" User, I don't have access to /dev/console. But I can run setleds with /dev/console as User root.
2) So for me it seems that if I wanted to use setleds with /dev/console in some script, I would want to make sure that the script runs as User root.
3) If you try to use a X-Windows oriented command, then you may need one or more things to be available to the command, such as a value for the DISPLAY environment variable, or a command option that tells the command which X Server to use.
4) If you use a command that expects access to a terminal device, but use it in an environment without a default terminal device, you'll need to connect the command to a terminal device explicitly.
5) If you test a command sequence in some sort of "terminal emulator" such as XTerm, Konsole, etc., the output of some commands may be affected by terminal mode settings related to tab character or other "white space" handling. If you then run the command in an environment without those considerations, the exact sequence of characters output by the command might be different.

Some of these factors can have a cumulative effect.

Beyond that, when I tested it in an XTerm window, the grep pattern you were using on the output of "xset -q", did not work for me. I had to change it to:
Code:
xset -q | egrep 'Caps Lock:[[:space:]]+off'
before it would find the phrase indicating that the Caps Lock was off.

Just in general, if I was trying to create a script like yours, I would start by manually testing individual pieces of it in an XTerm or some such, to make sure that each step worked. When I tried the grep pattern your were using, I did it without the option to "quiet" the output of grep. In that way I could determine whether or not the correct output of xset was being grabbed by grep, before using that grep pattern in a script. Yes, it's possible to just rely on the return code from the various forms of grep, but I prefer the comfort factor of seeing exactly what is output.

For me, either xset or setleds will turn LED's on or off if I run them manually from a terminal window ( as long as I become root to use setleds ). If they don't work for you when used manually in that way, then they can't be expected to work in a script. Depending on how you are causing your script to run, if you use xset you might need to do something such as use the -display option to specify an X Server which is communicating with your keyboard, in order to effectively connect the xset command to your keyboard. The solution to the problem could be as simple as that.

Last edited by rigor; 06-13-2015 at 01:50 PM.
 
Old 06-15-2015, 11:32 AM   #7
ondoho
Senior Member
 
Registered: Dec 2013
Posts: 4,176

Rep: Reputation: 835Reputation: 835Reputation: 835Reputation: 835Reputation: 835Reputation: 835Reputation: 835
isn't your script first switching the led off, only to then check whether it is on or off?
that seems wrong.

also, it should run in a loop, with a delay added, to constantly monitor your keyboard state, no?

also, as previous poster asked, how are you exactly invoking it and when?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux Mint KDE 14 problems with launch bar Davidicus Linux - Desktop 1 01-30-2013 09:15 PM
linux mint 11 gnome, lxde, kde problems mpru Linux - Newbie 2 09-05-2011 07:52 AM
How can I install new KDE to elim kbd locking after logon? jdtiede Linux - Software 2 10-01-2009 10:00 AM
problems with script writing to network share neocookie Linux - Newbie 2 03-22-2007 06:44 AM


All times are GMT -5. The time now is 04:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration