Button focus with key pressed

hi,
i want a button to get focus when the user presses a key on the keyboard. Thankz

have the focus anywhere in the textFields, press F1, and the focus will go to the button
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Testing
  public void buildGUI()
    JPanel p = new JPanel(new GridLayout(6,1));
    for(int x = 0; x < 5; x++) p.add(new JTextField(5));
    final JButton btn = new JButton("OK");
    p.add(btn);
    JFrame f = new JFrame();
    f.getContentPane().add(p);
    f.pack();
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
    KeyboardFocusManager.getCurrentKeyboardFocusManager()
     .addKeyEventDispatcher(new KeyEventDispatcher(){
        public boolean dispatchKeyEvent(KeyEvent ke){
          if(ke.getKeyCode() == KeyEvent.VK_F1 && ke.getID() == KeyEvent.KEY_PRESSED)
            btn.requestFocusInWindow();
          return false;
  public static void main(String[] args)
    SwingUtilities.invokeLater(new Runnable(){
      public void run(){
        new Testing().buildGUI();
}

Similar Messages

  • Back button focusing with the 6D

    I cannot get my 6D's back button focus (AF-ON) to work.  After following directions on Canon's website on how to set it up, the button just does not do anything.  Auto focus only works  with the shutter button.  I have the function set to "P" which I understood to be o.k.  Would resetting the camera to factory default help?  I would prefer not to have to send the camera to Canon for a one button repair.  The camera body was purchased new and is still under warranty.
    Thanks
    Solved!
    Go to Solution.

    It's super easy to set up BBF on a 6D....
    If you have already been changing some things and are unsure which ones, you might want to reset all the Custom Functions to factory default, before starting. (Note: There is a way to view which C.Fns are set and to what each is assigned... As you enter each of the four C.Fn groups, note the list in the lower LH corner of the LCD screen, that shows all the C.Fns in that group at a glance. The top line is the C.Fn number, the bottom line is the setting for each C.Fn.)
    To set up BBF....
    All you have to do is go into the menu, Custom Functions, C.Fn III-5, operations/others, button assignments. Select and enter that and then press "set" and this should take you right to the top of the LH column, which is a graphic illustration of the shutter release button. Press set again to enter this, then scroll to select the center option "start metering" and press "set" once more to select this choice, then press "menu" button several times to back out of the menus. Done!
    What this does is remove AF start from the shutter release button.  By default it's already assigned to the AF-On button on the back of the camera too, so BBF is set up and ready to use as soon as you remove it from the shutter release button.
    Metering and Image Stabilization will continue to start with a half press of the shutter release button or whenever you press the AF-On button. But AF will  only start with your thumb on the AF-On button. Using BBF, you now can use AI Servo most of the time, for stationary or moving subjects. If you want to "focus and recompose", simply focus and then lift your thumb off the AF-On button to leave focus where it is and recompose as you see fit. (Without BBF, you cannot do focus and recompose.... it will cause mis-focused shots.) You still might want to switch back to One Shot when you want high precision focus with stationary subjects.
    There is another option you may want to consider...
    Using the same Custom Function III-5, if you prefer you can swap the functions of the AF-On and * (AE Lock) buttons. I do this on my cameras for a couple reasons: One is that the * button is larger, more prominent, and closer to my thumb... so I prefer to use it for focusing. The other reason is that older Canon models that didn't have the AF-On button, you had to use the * button and that's the way I learned to use BBF (Note: In some cases there was an AF-On button on the body but not on the vertical grip, while older cameras simply didn't have an AF-On button at all, on the body or the grip).
    Alan Myers
    San Jose, Calif., USA
    "Walk softly and carry a big lens."
    GEAR: 5DII, 7D(x2), 50D(x3), some other cameras, various lenses & accessories
    FLICKR & PRINTROOM 

  • Help needed with key press commands

    Hi, im prety new to director so im guessing this is actually
    quite easy. I want the movie
    to move ahead one frame when a specific key is pressed, like
    S. How would i go about this?
    cheers

    Hey otakupunk:
    Search the forum for "key code"...also search the Adobe
    knowledge base, I believe that actually have a .dir file you can
    download with samples of what key code belongs to what key.

  • Start and stop movie with key press

    Can anyone please tell me how to do this?
    I'm very new to flash.
    Thankyou

    Place this code in the timeline that you are trying to control--pressing and releasing a key will play/stop depending on which mode it is currently in.
    stop();
    var isPlaying:Boolean = false;
    stage.addEventListener(KeyboardEvent.KEY_UP, keyUsed);
    function keyUsed(event:KeyboardEvent)
    if (isPlaying){
              stop();
         } else {
              play();
      isPlaying = !isPlaying;
    If you have other questions, try to anticipate what you might be asked and include those details.

  • Capturing Key press when the focus is not on VI. (event is not working).

    I have a VI, and found similar in Forums. I need to catch key down event, when the focus is not on VI.
    I attached the VI found in forum. Try to click on other window, for example on block diagram of VI, when VI is running and check if it works. The answer is no, it doesn't work. When you press any key, the led button must change it's state, but it does only when the focus is on VI's front panel. The question is how to control key press, when VI is minimized or inactive. Is it possible to catch key down or any similar event, when the focus is on any other application, not on VI?
    Solved!
    Go to Solution.
    Attachments:
    simulatekeyevent.vi ‏16 KB

    Have a look at the "Connectivity...input devices..." menu.
    For an example, open the example finder and look for "basic input demo". It should give you some ideas. Good luck!
    LabVIEW Champion . Do more with less code and in less time .

  • All web pages appear with a blinking cursor like in a "word document". i can't scroll down or up using the buttons of my key board. i have to pull down the scroll bar. something in settings has changed. how to correct it??

    all web pages appear with a blinking cursor like in a "word document". i can't scroll down or up using the buttons of my key board. i have to pull down the scroll bar. something in settings has changed. how to correct it??
    == This happened ==
    Just once or twice
    == 2 days ago.

    See also this article about caret browsing: http://kb.mozillazine.org/Scrolling_with_arrow_keys_no_longer_works
    In Firefox 3.6 and later versions you can disable the F7 shortcut that toggles caret browsing by setting the pref accessibility.browsewithcaret_shortcut.enabled to false.
    To open the ''about:config'' page, type '''about:config''' in the location (address) bar and press the Enter key, just like you type the url of a website to open a website.
    If you see a warning then you can confirm that you want to access that page.

  • Just tried to open iTunes, it won't, and I'm getting a blank window with an OK button. When I press the OK button, iTunes closes. what is going on?

    Have just tried to open iTunes, and I get a blank window (no dialogue, just a blank window) with an OK button. When I press OK, it closes down (not having opened anything) and shuts down. I've tried opening while holding down the option key, as I would to open with a new iTunes folder, and I"m getting the same empty window with an OK. Very frustrating, has anyone seen this, and can anyone help?  Thanks in advance,  Kate

    so, I called a friend who's more experienced than I am. He walked me through some steps, checking iTunes and we got the same dialogue -free window with the OK button. We then uninstalled iTunes, downloaded it afresh, and it opened with no problems. Seems to all be there, music, tv, etc. Not sure what the issue was, but it has stopped this problem.

  • Focus lost when pressing ALT key

    Hi,
    is there any way to prevent the focus to be lost when pressing ALT key in an AIR app??
    I tried event.preventDefault() in KEY_UP and KEY_DOWN events but it doesn't work
    when the user press the ALT key, the stage loses the focus
    thanks

    I am now having trouble with the same issue. I have tried removing the ALT key from the Input Map from my frame and the component I'm having trouble with. I have also tried setting my own action in the action map for this. We also tried listening for the key pressed event and putting focus back to the combobox where I want focus to remain. I am now working on trying to use the processKeyEvent method to handle this, but have not had any luck as of yet with that. If anyone has some help on this, I'd greatly appreciate it!

  • My iPhone 4S on iOS7 is stuck on Apple Logo, and it goes off, then back to Apple Logo again for hours. Pressing home button together with sleep button does not help. Plugging the phone to Mac and restart does not help either. What do I do?

    My iPhone 4S on iOS7 is stuck on Apple Logo, and it goes off, then back to Apple Logo again for hours. Pressing home button together with sleep button does not help. Plugging the phone to Mac and restart does not help either. What do I do?
    Have not backed up for months, so prefer not to lose the data.
    Thank you for your help in advance.

    Restore iPhone with iTunes on computer. Open iTunes on computer. Connect iPhone to computer with USB cable. Hold both Home and Power buttons until iTunes recognizes iPhone in Recovery Mode, usually about 20 seconds time of holding both buttons. Click the Update or Restore button that iTunes offers.

  • Movie contains buttons with keys assigned. Both work. If you click a button first, keys wont work.

    I created a project with a menu of 7 buttons. The client has the option to click the buttons to view a different scene or use keyboard events. Each scene has been assigned a number when using the keyboard. The mouse and keyboard events function successfully. But I noticed if I start the movie by using the mouse, then the keyboard events will not work any more. One of the keys is important since it is the only way to play frame to frame. Anybody have a clue why this happens?It is as if though I have to tell the client,  don't click the buttons or the keys won't work.
    Below are actionscripts for the movie. I added actionscript for each scene for the keyboard events. In the menu, the movieclip contains the actionscript for the mouse events.
    Scene 1:
    import flash.events.KeyboardEvent:
    stage.addEventListener(KeyboardEvent.KEY_UP, onKeyEvent1);
    function onKeyEvent1(e:KeyboardEvent):void {
    var character:String=String.fromCharCode(e.charCode);
    if (e.keyCode==72) {
      gotoAndStop(1,"master");
    if (e.charCode==49) {
      gotoAndPlay(1,"Distributor");
    if (e.charCode==50) {
      gotoAndPlay(1,"Mirka");
    if (e.charCode==51) {
      gotoAndPlay(1,"Farm");
    Menu Movieclips:
    master.addEventListener(MouseEvent.CLICK, mouseDownHandler1);
    function mouseDownHandler1(event:MouseEvent):void {
    MovieClip(root).gotoAndPlay(1,"master");
    distribute.addEventListener(MouseEvent.CLICK, mouseDownHandler2);
    function mouseDownHandler2(event:MouseEvent):void {
    MovieClip(root).gotoAndPlay(1,"Distributor");
    health.addEventListener(MouseEvent.CLICK, mouseDownHandler3);
    function mouseDownHandler3(event:MouseEvent):void {
    MovieClip(root).gotoAndPlay(1,"Mirka");
    mirka.addEventListener(MouseEvent.CLICK, mouseDownHandler4);
    function mouseDownHandler4(event:MouseEvent):void {
    MovieClip(root).gotoAndPlay(1,"Farm");
    transaction.addEventListener(MouseEvent.CLICK, mouseDownHandler5);
    function mouseDownHandler5(event:MouseEvent):void {
    MovieClip(root).gotoAndPlay(1,"Check2");
    transaction2.addEventListener(MouseEvent.CLICK, mouseDownHandler6);
    function mouseDownHandler6(event:MouseEvent):void {
    MovieClip(root).gotoAndPlay(1,"Check3");
    sceneA.addEventListener(MouseEvent.CLICK, mouseDownHandler7);
    function mouseDownHandler7(e:MouseEvent):void {
    MovieClip(root).gotoAndPlay(1,"Scene2");
    sceneB.addEventListener(MouseEvent.CLICK, mouseDownHandler8);
    function mouseDownHandler8(e:MouseEvent):void {
    MovieClip(root).gotoAndPlay(1,"Scene1");

    In addition to my question, I'm opening the SWF file in a utility program IrfanView which the client uses for offsite purposes. Therefore, the SWF works fine in Flashplayer 10. I can crossover using mouse and keyboard events. But in IrfanView, the program will not allow me to crossover mouse and keyboard events.

  • How do I get Siri on with out pressing that button on my I phone 5s

    How do I get Siri to talk with out  pressing that home button

    It does for everyone else.  You have to bring the phone up to your face (like making a call), activating the proximity sensor, for Siri to start.

  • Multiple key press's with Bluetooth Keyboard

    I'm ready to update my mac to a new G5 imac and fancy getting the wireless bluetooth keyboard and mouse with it.
    I have one question, has anyone experienced problems when holding down multiple keys on the keyboard and moving the mouse all at once (Using illustrator)?
    In my office I have the misfortune of having to use a PC, I have had a number of wireless keyboard and mouse combos, all of them when using Illustrator and photoshop can not handle multiple key press's and mouse movement.
    ie if I'm duplicating an object by dragging a copy of it across the screen, on the PC it will work but it always "releases" after a couple of seconds and drops the copy in the where I don't want it which gets very frustrating (this only happens with wireless).
    As I do a lot of work at home, I hoping this wont be the case with the mac's bluetooth K & M.
    thanks
    Russell

    The shift key works just fine on my Bluetooth keyboard.  So I would imagine this is an issue with your keyboard.

  • I like the "whites" and "blacks" sliders in lightroom with the option key pressed- is there a corresponding slider in aperture? thank you

    i like the "whites" and "blacks" sliders in lightroom with the option key pressed- is there a corresponding slider in aperture? thank you

    There's no exact analogue to the "whites" slider in Aperture.  But you could do it with curves if you wanted.  You can set the white point using the curves tool as well.

  • Catching Alt Key Press with the Key Down Filter Event

    I am writing an application that requires specific key combinations using ctrl, shift, and alt in addition to a function key (F1, F2, F3, etc).  The application works great except for when I try to catch an alt key press.  The alt key press does not seem to fire an event eventhough it is an option in the PlatMods cluster as well as the VKey enum.  When I press the alt key when my application is running the cursor changes to a normal mouse pointer from the usual finger pointer and prevents any other key presses from going through (in addition to not firing an event itself).
    I have tried completely removing the run-time menu, which doesn't seem to help.  I currently discard all keys after I handle them in my event structure.
    I really hope that the only solution isn't using a Windows DLL.  Any suggestions or ideas at all would be greatly appreciated.
    Thanks,
    Ames

     Hi Ames
    As Kileen has said Khalid has already given you a good solution to detect the ALT key.
    I have another approach that might let you stick to your event-driven approach. I suggest that you have another loop in your app that polls the keyboard using the Input Device utility vi's. When this poll loop sees an ALT + KEY combo it raises a dynamic user event and will be processed in your event structure. This means you can keep your key down filter event to process the CTRL + KEY and SHIFT + KEY events.
    Example attached in 7.1
    cheers
    David
    Attachments:
    Catching Alt Key Press Poll with Events(151551).vi ‏89 KB

  • Menu Bar - Sound Volume icon does not move with the press of the F key

    So I normally press the F keys to control the Sound Volume, right, just like everyone else does -
    but the Sound Volume icon in the Menu Bar at the top right does not respond to the F key at all!
    Isn't it supposed to react simultaneous with the press of the F key?
    I have to actually use my mouse click to make that volume go up and down.
    The actual volume does work with the F key, it goes up and down.
    But to make the 2 work in sync, I have to point the mouse to the one at the Menu Bar, fiddle with it, and then I can use the F key and both work together at the same time.
    If I haven't touched the Menu Bar one for a while and I only use the F key, the Menu Bar one doesn't move at all.
    It's weird. Any ideas?

    I see what Al is seeing. Normally I just use one method or the other. The Volume Adjustment keys provide visual feedback, so it's unnecessary to check the menu extra, i.e. the Volume menu bar icon, to see the same feedback.
    The slight delay in the reaction of the Volume menu extra, when open and while adjusting the volume with the Volume keys is normal.
    They're independent methods for adjusting the Volume setting. The Volume menu extra takes a moment to refresh when the slider is displayed and you're changing the setting via the Volume keys.
    Good luck!
    Dr. Smoke
    Author: Troubleshooting Mac® OS X

Maybe you are looking for

  • Problem starting smc

    Hello I am a total solaris newbie....all sympathy appreciated. I have a sun fire V120 running solaris 8, connecting through an xterm on Windows 200 I am trying to set up slices/partitions on my new disk, I am very unsure of running format commands et

  • Possible virus or spyware?

    Lately people on my contact list for Hotmail have been getting emails from me that send them a link to some website (not sure if it's the same site every time or not). I have not been sending the email and my friends seem to think I have a virus. I a

  • Orders05 sales order line item division mapping?

    Hi could anybody point me to the structure and field for the sales order line item division, found everything else but this one evades me

  • Restore a book project in iPhoto using Time Machine

    Using iPhoto '11 (9.1). My wife spent an age working a photobook for a friend's wedding. I found her in tears because just as she was about to order copies online she decided to try a different theme and it wiped the entire project. I said 'have no f

  • Trackpad preferences issues

    Hi, I've just recently updated to mavericks, but i can't change my trackpad preferences, 'cause when I try, it says 'Preferences Error'  'Could not load Trackpad preference pane.' What can I do?