I need to simulate keyboard button press in different window (media player to be specific).
I have the window name.
Is there a way to do that?
I'm sorry that i don't have anything to work with but i've been searching for anwswer for ages and still found nothing.
Try using the Robot class.
An example is here to find.
Related
I hope everyone is doing well.
I've built a hangman game with a swing gui and everything works well enough, HOWEVER I am trying to make a popup show up by constructing a new JFrame object when the user wins or loses with a "you lose" message or what have you. No problem, but I want a specific window to close when activating the button listener on the popup, or when the 'x' is clicked. Assume my program has 3 windows up, and I only want to close 2 of them with one click.
I tried stuff in the area of
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
But that specific one closes all the windows. If you want to see more specific sample code, I would happy to provide it, but it didn't seem necessary for this question.
Either way, I can't figure out how to do this. Is this possible using Swing?
Thank you so much in advance. You guys are always so helpful.
I am creating an application where I need that the keyboard pops up automatically without the need to click on the textbox in MIT APP INVENTOR 2.
I tried to focus on the textbox but it didn't work.
I created a screen2 and added a text box named TextBox1. Then in the blocks I added When Screen2.Initialize => call TextBox1.RequestFocus procedure. And it didn't work.
I found the solution and I am going to share it with you.
Just download the taifun tool extension and use it. Actually, it has the show keyboard option.
First choose the textbox to write in and then set the focus to that textbox and the keyboard will appear automatically. If don't you can try a actititystarter.
Greetings!
I have a simple game and when I run the program I have to click on the window before the game will accept user input.
When I play games like The Binding of Isaac they accept user input on the main menu without me ever clicking them.
Is there a way to set the focus of my keyboard to my game without clicking it first? There was another question on this: Have to click before pressing key , but it was left unanswered.
Call window.requestFocus(); after calling main.start() so you override any other focus request done in the meanwhile
If you have JFrame, or something like this (something inherited from java.awt.Component), you can try:
window.requestFocus();
Link to javadoc
EDIT:
In case of JFrame, I have found this question:
How to focus a JFrame?
One of the answer is the same as I advice
This can help you
Hello lovely community,
I'm Leon and at developing a little jump n run.I'm sitting since rough 24 hours on the laptop to find a solution for my problem. So my GUI starts up with a little menu, in it buttons titled for example with "start game". By clicking this button the game starts. BUT it starts in a new JFrame respectively it starts in a new window.. by playing the puzzle you can press escape for launching the pause menu. BUT again, it opens up in a new window. I need desperately a solution because I cant go on with coding.. Hope anyone can help me. THX Sorry for my English. Leon ;)
Don't use jbuttons. Whenever I make swing based game I use 1 jpanel in which I override paint method and create my own buttons (you'll need to give them functionallity with mouse/key listener, but that is not hard to figure out. If you want to see example code, look up the way minecraft creator does it (google "notch ludum dare" - all games have source code attached)
Wow, what a stupid question you might say. But is it possible? I have a mouse move event in a Jpanel and it works even when the application is not in focus, now can I have something like that for the clicking event or something similar. And most importantly I don't want other apps (even something like the main menu) to lose focus when i click on my app.
I thought of the system's event queue but i'm not sure where that would lead me to.
Thanks in advance.
[EDIT - the purpose]
I want to create an app that mimics the users interactions with the system in a later time. for example a user takes the mouse and clicks and writes and my app will mimic that in say 2 hours time. ofcourse i would need a system hook for the outer events but i wanted to avoid os-dependant code so i basically capture the screen, take it to my app,for example the user clicks on an icon in the captured picture and then for making that come to life, i translate the coordinates to the real icon and click it (with a Robot) and in this way i can capture the user's events in my own app. the problem occurs when the user clicks on the main menu or right clicks (he's doing that in my app, and my app does that to the system so my app is in between) and ofcourse the real main menu will lose focus when the user tries to click on one of it's items.
sorry for my english.
I'm still not sure I follow what you are trying to do. But the concept of an app getting focus when you click on it is fundamental to the GUI and I suspect rather difficult to get around.
I just found this:
Focusable Windows
To support palette windows and input methods, client code can prevent a Window from becoming the focused Window. By transitivity, this prevents the Window or any of its descendants from becoming the focus owner. Non-focusable Windows may still own Windows that are focusable. By default, every Frame and Dialog is focusable. Every Window which is not a Frame or Dialog, but whose nearest owning Frame or Dialog is showing on the screen, and which has at least one Component in its focus traversal cycle, is also focusable by default. To make a Window non-focusable, use Window.setFocusableWindowState(false).
In this doucment http://java.sun.com/j2se/1.5.0/docs/api/java/awt/doc-files/FocusSpec.html
That sounds like it might do what you want.