Click here to Skip to main content
Click here to Skip to main content
Go to top

How to get clipboard text in XNA

, 11 Feb 2013
Rate this:
Please Sign up or sign in to vote.
Here we get clipboard text to use in an XNA app.

Introduction

Here we want to get clipboard text in XNA, but as i did this similar to windows forms getting clipboard it gave me some error, that I should use STA thread. so here I'm explaining how to get clipboard text using System.Windows.Forms.

The clipboard is very important for users to paste their cheats and stuff in a game.

Using the code

We create a class with name clipboard and we put this code below in it.

Here I used another thread for my STA purposes, so I'm here avoiding conflict in multi threaded applications like games. The thread name is t. we wait until our thread get clipboard text and put it into clipboard.

After checking if there is any text in clipboard by Clipboard.ContainsText() we set clipboard string variable using the Clipboard.GetText() function.

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace Language_Learning_Application
{
    public class clsClipBoard
    {
        string clipboard="";
        public String GetClipboardText()
        {
            Thread t = new Thread(getClipboard);
            t.SetApartmentState(ApartmentState.STA);
            t.Start();
            while(t.IsAlive)
            {
            }
            return clipboard;
        }

        [STAThread]
        private void getClipboard()
        {
            if (Clipboard.ContainsText())
            {
                clipboard = Clipboard.GetText(TextDataFormat.UnicodeText);
                //Clipboard.SetText(replacementHtmlText, TextDataFormat.UnicodeText);
            }
        }
    }
}

So wherever we need clipboard text we do it like this:

public clsClipBoard myclsClipBoard = new clsClipBoard()
string clipboard  =myclsClipBoard.GetClipboardText();

It was easy more explanation would be given if there may be any question.

Points of Interest

Here we learned how to get clipboard text cause it's important in giving our application product keys or cheats or many other tasks.

History

  • Started it on 2/9/2013.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

F.moghaddampoor
Engineer
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
Education:
 
NODET 2003-2007
B.Sc. Electrical Power Engineering 2007–2012
 
Work:
 
T.G.D. Co.(Power,Software) July 2010 -February 2012
Artnous Co.(Power) July 2010 -February 2012
Savay Co.(Software) September 2011- July 2012

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 11 Feb 2013
Article Copyright 2013 by F.moghaddampoor
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid