Ubuntu: How to lock screen when mouse is moved to right-bottom corner?



Question:

I'd like to lock the screen when I move my mouse to the bottom-right corner.

Is there a compiz-configuration for that task?

I could not find anything in the compizconfig-settings-manager and system settings.


Solution:1

The simple tools to get mouse position is xdotool just install first and follow steps:

sudo apt-get install xdotool  

then we are useing xdotool getmouselocation --shell to see the current position of mouse: the result will be something like this:

X=845  Y=447  SCREEN=0  

or by running eval $(xdotool getmouselocation --shell) will put the position into shell variables X, Y and SCREEN. After that, we can access these variables with this:

echo $X $Y $SCREEN  

Well now we need a while loop to check mouse position on every time:

while true    do      [get mouse position]      [if position =bottom-right corner then lock screen]    done  

ok so our scripts would be like this:

#! /bin/sh  while true    do      eval $(xdotool getmouselocation --shell)      if [ $X -ge 1919 -a  $Y -ge 1079 ]; then          gnome-screensaver-command -l      fi    done  

save script named "lock.sh" in your home directory and run it by sh lock.sh and move your mouse to the bottom-right corner and see the result. cool!

explain:

We combine conditions by using certain operators. For the single-bracket syntax that we’ve been using so far, “-a” used for and. and “-o” for or. Example:

if [ $foo -ge 3 -a $bar -ge 10 ]; then  

The above condition will return true if $foo contains an integer g reater than or e qual to 3 (-ge 3) and also $bar contains an integer g reater than or e qual to 10. then run lock screen command line gnome-screensaver-command -l


Solution:2

I could not find any settings in the Compiz manager, nor in the Unity Tweak settings (which uses the same), but if you add the script below to your Startup Applications, it will check your screen resolution and your mouse position. If the mouse is within a marge from the corner (set in the head of the script) it will lock the screen.

How to use

  1. Install xdotool:

    sudo apt-get install xdotool  
  2. Paste the script below in an empty file, set the marge (pixels) within which you would like the hotcorner to act, save it as screenlock.py, make it executable for convenience reasons and add it to your startup applications (Dash > Startup Applications > Add). Add add the command:

    /path/to/screenlock.py  

The script:

#!/usr/bin/env python3    import time  import subprocess    marge = 3 # (pixels) increase to increase sensitivity    output = subprocess.check_output(["xrandr"]).decode('utf-8').strip().split()  idf = output.index("current")  res = (int(output[idf+1]), int(output[idf+3].replace(",", "")))    command = "gnome-screensaver-command -l"    while True:      get_pos = subprocess.check_output(["xdotool", "getmouselocation", "--shell"]).decode('utf-8').strip().split("\n")      pos = (int(get_pos[0][2:]), int(get_pos[1][2:]))      if res[0] - pos[0] < marge and res[1] - pos[1] < marge:          subprocess.Popen(["/bin/bash", "-c", command])      time.sleep(1)  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »