Results 1 to 3 of 3

Thread: How to create keyboard shortcuts to move the window to the center of the desktop?

  1. #1
    Join Date
    Aug 2009
    Beans
    2

    How to create keyboard shortcuts to move the window to the center of the desktop?

    Please help me how to create keyboard shortcuts to move the focused window to the center of the desktop!


    Thanks in advance

  2. #2
    Join Date
    Jun 2005
    Location
    Toronto, Canada
    Beans
    Hidden!
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: How to create keyboard shortcuts to move the window to the center of the desktop?

    Hello and welcome to the forums.

    To use the following script, you need to have the package xdotool installed. Look for it in the Software Centre or install it manually via:
    Code:
    sudo apt-get install xdotool


    Once thats done, with root privlidges, create the file /usr/local/bin/cw (from a terminal window):
    Code:
    sudo -i mousepad /usr/local/bin/cw
    ...or if you have an earlier version of Xubuntu with leafpad:
    Code:
    sudo -i leafpad /usr/local/bin/cw
    ...enter your password when prompted then copy/paste the following:
    Code:
    #!/bin/bash
    
    PANEL_HEIGHTS=0
    PANEL_WIDTHS=0
    
    SCREEN_WIDTH=`xdotool getdisplaygeometry | cut -d' ' -f1`
    SCREEN_HEIGHT=`xdotool getdisplaygeometry | cut -d' ' -f2`
    
    ACTIVE_WINDOW_ID=`xdotool getactivewindow`
    
    ACTIVE_WINDOW_WIDTH=`xwininfo -id $ACTIVE_WINDOW_ID | grep Width | cut -d' ' -f4`
    ACTIVE_WINDOW_HEIGHT=`xwininfo -id $ACTIVE_WINDOW_ID | grep Height | cut -d' ' -f4`
    
    xpos=$((($SCREEN_WIDTH-$PANEL_WIDTHS-$ACTIVE_WINDOW_WIDTH)/2))
    ypos=$((($SCREEN_HEIGHT-$PANEL_HEIGHTS-$ACTIVE_WINDOW_HEIGHT)/2))
    
    xdotool windowmove $ACTIVE_WINDOW_ID $xpos $ypos
    ...and save the file.

    Next, make the file executable via:
    Code:
    sudo chmod +x /usr/local/bin/cw
    You can now test the script by opening a terminal window, moving it away from centre, then executing the command:
    Code:
    cw
    ...and the window should centre.

    The script acts on the currently focused window, so by adding it to a keyboard shortcut, it will act on whichever window is currently active. To create a keyboard shortcut in Xfce, go to Settings Manager -> Keyboard -> Application Shortcuts, click on the "Add" button, for the command enter:
    Code:
    /usr/local/bin/cw
    ...press "Ok" then press the keyboard combination you want to use.



    Note: The script includes two variables:
    PANEL_HEIGHTS=0
    PANEL_WIDTHS=0
    Feel free to adjust these by giving them the values (in pixels) of the total widths/heights of your panels if you want the window to centre relative to the panels (not screen dimensions).

  3. #3
    Join Date
    Aug 2009
    Beans
    2

    Re: How to create keyboard shortcuts to move the window to the center of the desktop?

    Amazing! Thank you very much

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •