0

My application uses the clipboard operations to perform the task. I am trying to use the value from Microsoft Excel and from the notepad. When I select these values and I press a hotkey that is registered from my application say Ctrl + 2 , when I try to debug and check the Clipboard.GetText() returns null and when I press a hotkey it performs nothing.

private void ReceiveHotKey(string hotKeyID)
{
    if (options == null || options.Visible == false)
    {
        clipBoardText = null;
        prevStringObject = null;
        prevDataObject = null;
        hotKeyTrialNumber = 0;
        this.myHotKey.HotKeyPressed -= new HotKey.HotKeyPressedEventHandler(ReceiveHotKey);
        try
        {
            //get the previous value from the clipboard
            if (Clipboard.ContainsText())
            {
                prevStringObject = Clipboard.GetText();
            }
            else
            {
                prevDataObject = Clipboard.GetDataObject();
            }
            //copy the data and store it in the clipboard
            SendKeys.SendWait("^c");
            System.Threading.Thread.Sleep(500);

            if (Clipboard.ContainsText())
            {
                clipBoardText = Clipboard.GetText(); // returns null
                Clipboard.Clear();
                ShortcutKeyVisualize(hotKeyID);
                //restore the previous value to the clipboard                    
                RestoreClipboard();
            }
            else
            {
                Log.ErrorFormat("Error!!!", language.CLIPBOARD_BUSY);
                //MyMessageBox.Log("Error!!!", m_objLanguage.CLIPBOARD_BUSY);
                RetryClipboardOperation(hotKeyID);                    
            }
        }
        catch (Exception e)
        {
            if (string.IsNullOrEmpty(prevStringObject) && null != prevDataObject)
            {
                Log.Error(language.CLIPBOARD_PROCESS_USER);
                //MyMessageBox.Log(m_objLanguage.CLIPBOARD_PROCESS_USER);
                return;
            }
            else if (string.IsNullOrEmpty(clipBoardText))
            {
                MessageBox.Show(language.CLIPBOARD_PROCESS_AUTO);
                //MyMessageBox.Log(e.Message, m_objLanguage.CLIPBOARD_PROCESS_AUTO);
                Log.ErrorFormat(e.Message, language.CLIPBOARD_PROCESS_AUTO);
            }
            else
            {
                MessageBox.Show(language.CLIPBOARD_PROCESS_AUTO);
                //MyMessageBox.Log(e.Message, m_objLanguage.CLIPBOARD_PROCESS_AUTO);
                Log.ErrorFormat(e.Message, language.CLIPBOARD_PROCESS_AUTO);
            }

            if (hotKeyTrialNumber < 5)
            {
                hotKeyTrialNumber = hotKeyTrialNumber + 1;
                RetryClipboardOperation(hotKeyID);
            }
            else
            {
                MessageBox.Show(language.CLIPBOARD_BUSY);
                ApplicationRestart();
            }
            //MyMessageBox.Log(e.Message, e.StackTrace);
            Log.ErrorFormat(e.Message, e.StackTrace);
        }
        finally
        {
            this.myHotKey.HotKeyPressed += new HotKey.HotKeyPressedEventHandler(ReceiveHotKey);
        }
    }    
}

// Restores the clipboard data
// after the hot key press event
// is completed.
private void RestoreClipboard()
{
    //do not restore the old data in the
    //clipboard if a new data is already 
    //present in the clipboard
    if (!(Clipboard.ContainsAudio() || Clipboard.ContainsImage() || Clipboard.ContainsText()))
    {
         //check the data object and assign if not null
         if (prevDataObject != null)
         {
              Clipboard.SetDataObject(prevDataObject);
         }
         //now check the string object and assign it 
         //to the clipboard if not null
         if (prevStringObject != null)
         {
              Clipboard.SetText(prevStringObject);
         }
    }            
}

Am I missing out something?

VMai
  • 10,156
  • 9
  • 25
  • 34
roopini n
  • 503
  • 2
  • 7
  • 29

0 Answers0