User Tools

Site Tools


k20:k20.4:start

20.4.0 Clipboard

The Clipboard class (gb.qt4) is static and can be used to manipulate the system's clipboard. The clipboard is a storage area for the simple exchange of data (text and images) between programmes. First, the data is copied or cut and stored in the clipboard. Then the data stored in the clipboard can be pasted into suitable Gambas components for text or images, thus realising the copying or moving of data. When the Gambas programme that uses the clipboard is terminated, the clipboard object is also destroyed and with it its contents. A remedy is to install a clipboard manager:

20.4.0.1 Constants

The Clipboard class uses 3 constants to indicate the type of content of the clipboard:

ConstantConstantDescription
None0The clipboard content could not be interpreted as either text or image.
Text1The clipboard content was interpreted as text.
Image2The clipboard content was interpreted as an image.

Table 20.4.0.1.1 : Constants of the Clipboard class

20.4.0.2 Properties

The Clipboard class has these properties:

PropertyData typeDescription
FormatStringReturns the text format of the text stored in the clipboard or NULL if the clipboard is empty.
FormatsString[]Returns a string array with the MIME types of the data stored in the clipboard.
TypeIntegerReturns the type of the content of the clipboard. This is either Clipboard.Text (=1) for text or Clipboard.Image (=2) for an image or Clipboard.None (=0) if the clipboard is empty.

Table 20.4.0.2.1 : Properties of the Clipboard class

20.4.0.3 Methods

The Clipboard class has three methods, the description of which can be found in the next table:

MethodDescription
ClearThe call to the method Clipboard.Clear deletes the contents of the clipboard.
Copy ( Data AS Variant [ , Format AS String ] ) Copies data to the clipboard. If the data is of the type 'String', you can specify the MIME type with the parameter 'Format', for example with “text/plain”.
Function Paste ( [ Format As String ] ) As VariantInserts the content of the clipboard at the intended position. You can specify the MIME type using the optional Format parameter. NULL is returned if the content of the clipboard is empty or cannot be interpreted as text or image.

Table 20.4.0.3.1 : Methods of the Clipboard class

20.4.0.4 Mime types

Data copied from a programme to the clipboard usually exists there in several formats.

  • You have the possibility to read and display the formats of the data stored in the clipboard - displayed with the corresponding MIME type - via the value of the Clipboard.Formats property.
  • For example, a screenshot - taken with the key combination CTRL+Print key - is stored in the clipboard in 7 formats: image/x-MS-bmp, image/x-bmp, image/bmp, image/tiff, image/jpeg, image/png and application/x-qt-image (→ Ubuntu).
  • In contrast, a copied text from a web page - displayed with Firefox - had these text formats: text/html, text/_moz_htmlcontext, text/_moz_htmlinfo, text/plain and text/x-moz-url-priv.

If you know the formats of your preferred (source) applications, then you can later already put the texts or images in a suitable format in the clipboard or paste them from it in the specified format into the (target) application that will further process this data.

20.4.0.5 Inserting data into the clipboard

There are several ways to put data on the clipboard or in the clipboard. What all data have in common is that under Gambas you can only paste text or images into the clipboard and from there into a (target) application. This does not apply to text or image files! You must first assign their content to a property of a usable Gambas object with the data type Picture or Image.

Example 1 - Copy a picture from a file to the clipboard (data type Image):

  Clipboard.Copy(Image.Load("Pictures/fraktal_1.jpg"))
  Clipboard.Copy(Picture.Load("Pictures/fraktal_1.jpg").Image)

Example 2 - Putting a picture from a picture box on the clipboard:

  If PictureBox1.Picture <> Null Then
     Clipboard.Copy(PictureBox1.Picture.Image)
  Endif

Example 3 - Display and save a desktop screenshot

The following procedure returns a screen copy that is immediately displayed in a PictureBox and then saved in a specified directory as a png graphic with the highest quality (→ 100 %):

[1] Public Sub btnGetScreenShot_Click()
[2]   FMain.Hide()
[3]     Wait 0.1 '-- In practice, increase to 3 seconds!
[4]     PictureBox1.Picture = Desktop.Screenshot()
[5]     PictureBox1.Picture.Save(User.Home &/ "Bilder/current_screenshot.png", 100)
[6]   FMain.Show()
[7]
[8] '-- Alternative without direct display -> Insert save dialogue option
[9] '-- FMain.Hide()
[10] '-- Wait 0.05
[11] '-- Desktop.Screenshot().Save(User.Home &/ "Bilder/current_screenshot.png", 100)
[12] '-- FMain.Show()
[13]
[14] End

Under KDE and Gnome, screenshots can be created by simply pressing the Print key. With the key combination Alt+Print key you place an image of the active window in the clipboard, the entire desktop in contrast with CTRL+Print key. If you use a special programme to take a screenshot, its programme options may override the above-mentioned effects of the keys or key combinations. These programmes include Shutter and KSnapshot - but also Gimp (File> Create> Screenshot).

The console programme 'Gnome-Screenshot' is interesting in connection with the instructions SHELL and EXEC:

  gnome-screenshot -w -b -c --delay=2
  gnome-screenshot -i
-c, --clipboard                Sending a recording directly to the clipboard
-w, --window                   Record only one window instead of the whole screen
-b, --include-border           Include the window frame in the screenshot
-d, --delay=Sekunden           Take a screenshot only after the specified time [Time in seconds].
-i, --interactive              Set options interactively

Example 4 - Screenshot of the active top-level window.

  SHELL "gnome-screenshot -c -w -b --delay=1" Wait
  PictureBox2.Picture = Clipboard.Paste().Picture

The screenshot of the top-level window is displayed in a PictureBox.

Download

Chapter

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k20/k20.4/start.txt · Last modified: 22.10.2023 by emma

Page Tools