title: DispatchKeyEvent to listen for Spacebar being pressed
body: <p>I am working on an Android project and I need to check when the spacebar is pressed sos I can execute a certain function.</p>
  <p>The problem is, it was working on the emulator but not on my actual device. I think it might be because my emulator was using a physical keyboard, not the onscreen virtual keyboard but when testing on an actual device, its using a virtual keyboard.</p>
  <p>I'm trying the dispatch keyevent</p>
  <pre><code>@Override
      public boolean dispatchKeyEvent(KeyEvent event)
      {
          
          if (event.getKeyCode() == KeyEvent.KEYCODE_SPACE
                  &amp;&amp; event.getAction() == KeyEvent.ACTION_UP)
          {
              QueryEditor queryEditor = (QueryEditor)getSupportFragmentManager().findFragmentById(R.id.fragment_queryEditor);
              queryEditor.formatQueryText();
              return true;
          }
          }
  </code></pre>
  <p>I've also tried the on key down</p>
  <pre><code>@Override
      public boolean onKeyDown(int keyCode, KeyEvent event)
      {
          if (keyCode == KeyEvent.KEYCODE_BACK)
          {
              disconnectDatabase();
          }
          else if (keyCode == KeyEvent.KEYCODE_DEL)
          {
              QueryEditor queryEditor = (QueryEditor)getSupportFragmentManager().findFragmentById(R.id.fragment_queryEditor);
              queryEditor.formatQueryText();
          }
          
          return super.onKeyDown(keyCode, event);
      }
  </code></pre>
  <p>Neither of these get fired though unless the back button is pressed but I need the spacebar to trigger the event.</p>
  <h2>Update</h2>
  <p>Below is my code for how QueryEditor Fragment is created and the event handler is created</p>
  <pre><code>@Override
      public void onActivityCreated(Bundle savedInstanceState)
      {
          super.onActivityCreated(savedInstanceState);
          
          iQueryEditor = (IQueryEditor)this;
          iErrorHandler = (IErrorHandler)this;
          
          txtQueryEditor = (EditText)getActivity().findViewById(R.id.query_txtQueryEditor);
          
          btnSubmitQuery = (ImageButton)getActivity().findViewById(R.id.query_btnPerformQuery);
          btnClearQuery = (ImageButton)getActivity().findViewById(R.id.query_btnDeleteQuery);
          
          txtQueryEditor.addTextChangedListener(new QueryTextChanged(getActivity(), txtQueryEditor, iQueryEditor));
          setDatabaseUsed();
          txtQueryEditor.setOnEditorActionListener(new OnEditorActionListener() {
              
              @Override
              public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                  if ((event != null &amp;&amp; (event.getKeyCode() == KeyEvent.KEYCODE_ENTER))
                          || actionId == EditorInfo.IME_ACTION_DONE)
                  {
                      executeQuery();
                      return true;
                  }
                  else if (event.getKeyCode() == KeyEvent.KEYCODE_SPACE &amp;&amp; event.getAction() == KeyEvent.ACTION_UP)
                  {
                      Toast.makeText(getActivity(), &quot;Space Bar Pressed&quot;, Toast.LENGTH_SHORT).show();
                      return true;
                  }
                  return false;
              }
          });
          
          btnSubmitQuery.setOnClickListener(mBtnSubmitQueryListener);
          btnClearQuery.setOnClickListener(mBtnDeleteQueryListener);
      }
  </code></pre>
  <p>txtQueryEditor is the EditText that I am trying to receive the space bar event on.</p>