Click here to Skip to main content
15,867,939 members
Articles / Programming Languages / Java
Article

Toggle the Multi Monitor Setting in Windows XP

Rate me:
Please Sign up or sign in to vote.
2.60/5 (2 votes)
24 Apr 2008Apache2 min read 63.1K   559   9   11
Automate - "Extend my Windows desktop onto this monitor" using Java
Image 1

Introduction

If you use Windows XP and a dual monitor, this article will prove to be useful. The code will launch the display control 'applet' and send it keystrokes (the same way you would if you use keyboard navigation) to extend the monitor to the secondary attached monitor.

Background

It is a known fact that using multiple monitors can help improve your productivity. Some people find it confusing when using more than one monitor - this article is not meant for them. You can dock, let's say your email client/calendar on one monitor and your source code editor (Eclipse) on the other one. Or you can dock your music player on one monitor and the article you are pretending to read on the other.

Using the Code

The code is a complete application packaged into a single class file. All you have to do to use the code is:

Java
java MultiMon  

A brief explanation of how the code achieves the desired result is discussed below:

  1. First the display panel is launched using:

    Java
    Runtime.getRuntime().exec("control.exe desk.cpl,@0,3");  

    This will also cause the third tab in the display control panel to be selected.

  2. Create an instance of the java.awt.Robot class, this class will help us send the desired key strokes to the display control panel.

  3. We then use...

    Java
    robot.keyPress(KeyEvent.VK_D + KeyEvent.ALT_MASK);  

    ... to send keystrokes, as in the case above ALT+D to the display control panel. And so on...

  4. The keystrokes the code sends out is the same as the ones that you would use if you were to use the keyboard only to toggle the monitor.

  5. Invoke the program using the batch file or compile the source code and run the generated class file.

Note: For ease of use, the class is not included in any package, but is in the default package. However it is a recommended approach to always use packages for the classes.

Of course, you need to make sure that JDK is installed on your machine.

Points of Interest

  1. Ideally you would like to implement such an application in a Win32 technology using the Windows API to achieve the necessary result. But then, I've just used Java...
  2. I just learnt how lazy I can get and I'm not the only one looking at the Google results for requests for such a solution. I spent less than an hour researching and writing this code. In case you have any positive comments, I'd be glad to hear them.
  3. Certain graphic cardset manufacturers like nVidia have software that provides 'enhanced' desktop extensions. These may be better than the extension that XP provides.

History

  • 16th April, 2008: Article created
  • 23rd April 2008: Article updated based on comments

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


Written By
Product Manager
India India
Imran is a software professional with 9 years of development experience and has worked on JAVA, VB6, VC6, C# & some JavaScript.

He is interested in Hi-Performance code, OO Methodology, OS API's, Optimizing Windows, Tweaking IE.

For the last few years Imran has specialized in developing robust, hi-performance applications & GUI's (Swing) for MNC's.

http://techspot121.blogspot.com/

Comments and Discussions

 
Questioncan it be doing in background? Pin
sotailok13-Dec-11 4:35
sotailok13-Dec-11 4:35 
QuestionWhy?? Pin
Shog923-Apr-08 10:09
sitebuilderShog923-Apr-08 10:09 
AnswerRe: Why?? Pin
Scuttle23-Apr-08 17:05
Scuttle23-Apr-08 17:05 
GeneralRe: Why?? Pin
Imran Ebrahim23-Apr-08 20:16
Imran Ebrahim23-Apr-08 20:16 
AnswerRe: Why?? Pin
Imran Ebrahim23-Apr-08 20:13
Imran Ebrahim23-Apr-08 20:13 
GeneralRe: Why?? Pin
Shog924-Apr-08 2:58
sitebuilderShog924-Apr-08 2:58 
GeneralRe: Why?? Pin
Imran Ebrahim24-Apr-08 3:51
Imran Ebrahim24-Apr-08 3:51 
Shog9 wrote:
I got that. I asked, "why?"

1) You got that, did you ? Maybe not. However if you do read the article you may know why.
2) Well what exactly do you do to make your applications work well on multi-monitor systems ?
GeneralRe: Why?? Pin
Shog924-Apr-08 3:54
sitebuilderShog924-Apr-08 3:54 
GeneralRe: Why?? Pin
Imran Ebrahim24-Apr-08 4:06
Imran Ebrahim24-Apr-08 4:06 
GeneralI'll make this dead simple: Pin
Shog924-Apr-08 5:11
sitebuilderShog924-Apr-08 5:11 
GeneralRe: I'll make this dead simple: Pin
Imran Ebrahim24-Apr-08 20:32
Imran Ebrahim24-Apr-08 20:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.