Java detect when user closes down the laptop screen? - java

I have a little program where i need to somehow detect when the user just closes down his/her laptop screen. So; not turning of the entire computer but rather just closing down the screen.
Ive got a chat program and i need to make the user go offline when the screen is closed. For some reason my current socket connection is still alive when i just close down the screen.
Any ideas?

You can give a look to that library :
http://www.codeproject.com/Articles/6590/PC-StandBy-Detector-for-Java-Apps
It is really simple to use and worked well for me. (Window only)

Related

Java Console Application Locked

Java is not my main programming language so my expertise are generally limited in this area.
Scenario
This app is supposed to execute for indefinitely like a service or daemon. Its main purpose is to process some rows in DB as they are inserted by some other process.
The app at times Spits out log messages through System.out.println. It also uses some other log files. I was earlier using log4j but that is disabled for the moment.
Also I am running this in Windows Server environment, if that helps. I cant use linux for now.
Problem
The problem is that, on rare occasions, I find the application in staging/prod env becomes locked or does not seem to process any DB rows further. After I press enter a few times on screen it moves ahead and start processing as usual. This is strange because I am not expecting any input from the user. Any input the app has is either from command line or from DB.
Also I know for sure that its not a DB issue. The app is not waiting for any table lock. It may have to do with some file locking or System.out stream being locked. The later I did not think of possibility until I see that some keystroke on screen 'wakes' the application.
So I am wondering if someone may point me in a direction where I can debug this further.
Microsoft in all its infinite wisdom has made the console window in such a way that it allows you to select a rectangular area of characters with the mouse. So far so good.
And supposedly in order to make it easier for the user to select text while text may be scrolling, they (in their infinite wisdom) freeze the process hosted by the terminal window if it attempts to emit text while you are selecting text. (*)
And it just so happens that if you do as little as click a console window with the mouse, they (in their infinite wisdom) put the window in text selection mode, as witnessed by the presence of a little white square.
Which is very unfortunate, because clicking on a window is what we also do in order to simply bring the window to the foreground. But there is a huge gotcha here: if the console window was not already on the foreground, then clicking it brings it to the foreground. But if the console window was already on the foreground, then clicking it enters selection mode. (And of course most people rarely know, or care, or want to be bothered, with whether the window is already on the foreground or not.)
So, to conclude: try not clicking your window. Click only the caption if you have to. If you do accidentally click on the window, then press [Enter] or click again to make the little white selection rectangle go away.
Because for as long as the little white selection rectangle is there, your program is frozen.
(*) Now, they could have easily frozen the display of the window while selecting without freezing the process; they could have made it so that the output of the process keeps being buffered, and then dumped on the screen once selection is done, but I guess they were too busy with their plans for world domination to get around to fixing this. I cannot even begin to think how much time (= money) must have been wasted all over the world so far due to microsoft console windows sitting idle in selection mode while people are under the impression that they are crunching data.
this is the way to do it
go tp properties and un-check : "quick edit mode"

How can I make my program keep working in the background?

I can't really show anything here I think.
It's not a problem in my code I think.
What I mean is when I'm running my program directly from my android device, or when it's connected to the USB on my PC through android studio in debug mode, or regular running in all cases when my program is running and then I open a new app on my phone or if I just move to the whatsapp to type something when I want to get back to my program I see it was close. And I need to run the program over again.
How can I keep my program to be running all the time in the background so I can get back to it and no need to run it over again?

Quitting Java program renders Mac OS X window switching non-functional

On the Mac where I do my programming, I have an app installed called BetterTouchTool that allows me to bind keyboard shortcuts to 'gestures' on my trackpad. I have bound a gesture (that doesn't have an Apple default) to the shortcut command+Q, which quits the active program. I have found this very convenient for quickly quitting programs that I am testing, so that I can get back to writing code.
However for the graphical game that I am programming, this action makes it so that I can't use a three finger swipe to switch between windows (several other gestures are also disabled). This will also happen if I go up to the menu and click quit or if I click the red x close button. The fix is to either restart my computer or (and this is really weird) to click the in-game close button which calls System.exit(0);. I know that most players will be using this but I still want to figure out why this is happening. In addition games like Minecraft on my computer don't do this.
I am using a JFrame with setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); in the code. In an attempt to fix the issue I have tried registering a com.apple.eawt.QuitHandler that calls System.exit(0); in the handler.
I have no idea what would be considered relevant code here, so whatever you would like to see, just let me know.
I figured it out. There was one rouge Thread.sleep() call in my JFrame that was somehow effecting my system.

Start A Program On A Certain Screen

I'm making a program which for myself personally, would be very useful and remove one of the many things that annoy me. :p To sum it up, I'm making a java application to register a global keyboard+mousemotion listener, and when a certain key on the keyboard is pushed it opens a program. What my java program would do, is before it goes directly to the program to open it, the java program will intercept the call and say 'open on the screen that contains the cursor'. And then will continue to launch the program, but make it open on screen 1,2,3,4 etc.
What my question is, how can I make for example google chrome open on screen 1,2,3,4 etc. Is it possible with java?
Thanks
You can use the java Runtime class. Here is an example Maybe you can tell your operating system which screen you want via terminal. (In linux DISPLAY env variable.)

How to prevent user close my application? (Linux Touchscreen apps)

I would like to have an application running in a linux box with a touchscreen monitor.
I don't want to use Flash player (with full window size), so my question is, what I should use instead of a Flash Player?
This box is gonna be in a place with a lot of kids, so they can close the application and shutdown the machine XD.
I'm not a Sr. Java developer, so I don't know if theres something over there to block the 'close window' feature in an java application.
Thanks in advance!
Assuming you're using swing, you could use Frame.setUndecorated(true) and maximize it. There is also the option of exclusive full screen mode. From there, you'll have to unbind any global keys that map to window-exiters/taskmanagers/other-utils.
You may also want a watcher/spawner script setup to make sure your app stays up. I bet there is a more elegant way to do this but most window managers try to avoid an app doing this due to malicious possibilities.

Categories