8 Replies Latest reply on Aug 5, 2009 7:43 AM by Gregory Lafrance

    Binding the function of a keyboard key to an application button

    jimmyoneshot Level 1

      Is it possible to bind the function of a key press to a clicked button within a flex/air application?

       

      The reason I'm asking is my application is going to be placed on a Linux operating system but the guy who owns this system has a program on the desktop that is binded to the F10 key meaning whenever F10 is pressed this program is opened. What he wants to be able to do is switch from the application to this program from within the application itself whilst still keeping my created application open in the background.

       

      The problem is this system will soon be operated by remote control so there will be no F1-12 keys avaiable just letters and numbers. So what I need to do is create a button that once clicked activates the F10 key. The idea being that the user can switch between this program and the interface without actually seeing the desktop at all. Is this at all possible?

       

      Is so how could it be applied to this simple button in the following code?:-

       

       

      <?xml version="1.0" encoding="utf-8"?>

      <mx:WindowedApplication

       

       

       

       

      xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"

      >

       

       

       

       

       

      <mx:Button id="switch" horizontalCenter="0" verticalCenter="0" label="Switch"

      />

       

      </mx:WindowedApplication>

        • 1. Re: Binding the function of a keyboard key to an application button
          jimmyoneshot Level 1

          Actually come to think of it, is it at all possible to get any key presses made within an Adobe Air application to interact with the OS/desktop at all? What I mean is if I do actually get this button's click property to activate the F10 key wouldn't that interaction take place just within the interface rather than externally?

           

          I take it if this sort of interaction is impossible then it's pretty hopeless and I'll have find another method.

          • 2. Re: Binding the function of a keyboard key to an application button
            Gregory Lafrance Level 6

            Currently you cannot execute native applications from a Flex/AIR application, though I have heard this may change in AIR 2.0.

             

            I think the only option is to write something to file, and have some other program (written in Java???) looking for that file, and when it appears, that other program does what is necessary outside your application.

             

            A major hack, but just trying to provide ideas.

             

            If this post answered your question or helped, please mark it as such.

            1 person found this helpful
            • 3. Re: Binding the function of a keyboard key to an application button
              jimmyoneshot Level 1

              Yes I think that would be beyond me unfortunately

               

              Thanks for the help anyway though. It's strange that this function wasn't implemented though as it does seem feasible that some users may want an interface that could for example function as a sort of desktop in which they could place all of their icons and when each icon is clicked it will open that specific program or something along those lines.

               

              The guy who I'm making the application for simply needs a button which causes a key value to be outputted to the Operating System so when you click on the button it would have the same effect as pressing that specific key on the desktop itself even if it was a regular letter or number key but if this is impossible I guess I'll have to look for a workaround.

               

               

              • 4. Re: Binding the function of a keyboard key to an application button
                jimmyoneshot Level 1

                If this is not possible though would you happen to know how to bind the click of a button to a key press within the application itself?

                 

                For example if the user selected the text input in this application and clicked the button the letter 'a' would be outputted to the textbox what code would be needed to be put within the click statement of the button to achieve this? I don't mean simply put the text 'a' in the box I mean to like activate the a key itself so if the user clicked the button 5 times the text input content would be aaaaa. Or would this be very difficult to do?

                 

                 

                 

                 

                 

                 

                <?xml version="1.0" encoding="utf-8"?>

                <mx:WindowedApplication

                 

                 

                xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

                 

                 

                 

                <mx:VBox x="359" y="262" horizontalCenter="0" verticalCenter="0"

                >

                 

                 

                <mx:Button id="LetterButton" label="Button"

                />

                 

                 

                <mx:TextInput

                />

                 

                 

                </mx:VBox>

                 

                </mx:WindowedApplication>

                • 5. Re: Binding the function of a keyboard key to an application button
                  Gregory Lafrance Level 6

                  In theory you could create an instance of KeyboardEvent, and set the keycode. But then what to do with it. Don't think you can do anything with it then:

                   

                  <?xml version="1.0" encoding="utf-8"?>
                  <mx:WindowedApplication
                    xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
                     keyDown="getChar(event);">
                    <mx:Script>
                      <![CDATA[
                        private function addChar():void{
                          this.dispatchEvent(new KeyboardEvent(KeyboardEvent.KEY_DOWN, true, false, 0, Keyboard.A));
                        }
                       
                        private function getChar(evt:KeyboardEvent):void{
                          trace("got event");
                          txt.text = String(evt.keyCode);
                        }
                      ]]>
                    </mx:Script>
                    <mx:VBox x="359" y="262" horizontalCenter="0" verticalCenter="0">
                      <mx:Button id="LetterButton" label="Button" click="addChar();"/>
                      <mx:TextInput id="txt"/>
                    </mx:VBox>
                  </mx:WindowedApplication>


                  If this post answers your question or helps, please mark it as such.

                  1 person found this helpful
                  • 6. Re: Binding the function of a keyboard key to an application button
                    jimmyoneshot Level 1

                    That seems to work within the application but I just wish it were possible to output it to the desktop.

                     

                    This is helpful to me though as it allows me to show the guy who I'm developing it for that I have created a button that has the action of a key binded to it but it just can't be outputted to anything besides the interface itself.

                     

                    Do you know if there are any forums where anyone would be able to use Flex and javascript together who could make a script for me to do this particular action i.e. output a keypress to an operating system at the click of a button within an application, which could be inserted into a scrript tag in my application? It's just that my experience at using java especially within Flex is nil

                    • 7. Re: Binding the function of a keyboard key to an application button
                      Gregory Lafrance Level 6

                      I'm thinking Javascript might have the same issue, but maybe you mean Java (two totally different animals).

                       

                      You need the java.awt.Robot class. The keyPress() method will simulate a keypress.

                       

                      You can probably figure this out pretty quick.

                       

                      If this post answers your question or helps, please mark it as such.

                      • 8. Re: Binding the function of a keyboard key to an application button
                        jimmyoneshot Level 1

                        Lol shows how little I know about Java or Javascript then

                         

                        Cheers. I'll give it a go.