CLIPBOARD$: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
{{DISPLAYTITLE:_CLIPBOARD$}}
{{DISPLAYTITLE:_CLIPBOARD$}}
The [[_CLIPBOARD$]] function returns the current Operating System's clipboard contents as a [[STRING]].
The '''_CLIPBOARD$''' statement copies the [[STRING]] value into the system clipboard.




{{PageSyntax}}
{{PageSyntax}}
:{{Parameter|result$}} = [[_CLIPBOARD$]]
: [[_CLIPBOARD$]] = {{Parameter|string_expression$}}




{{PageDescription}}
{{PageDescription}}
* Text returned can contain the entire contents of a copied file or web page or text from a previous [[_CLIPBOARD$ (statement)|_CLIPBOARD$]] statement.
* {{Parameter|string_expression$}} is the string value to be sent to the clipboard.
* The string returned can also contain formatting like CRLF ([[CHR$]](13) + [[CHR$]](10)) end of line characters.
* The string value will replace everything previously in the clipboard.
* Assemble long text into one string variable value before using this statement.
* Add CHR$(13) + CHR$(10) CRLF characters to move to a new clipboard line.
* When copying text files, end line CRLF characters 13 and 10 do not have to be added.
* Numerical values can be converted to strings using [[STR$]], [[_MK$]], [[MKI$]], [[MKL$]], [[MKS$]], [[MKD$]], [[_BIN$]], [[HEX$]] or [[OCT$]].
* The clipboard can be used to copy, paste and communicate between running programs.
* The clipboard can be used to copy, paste and communicate between running programs.




{{PageExamples}}
{{PageExamples}}
''Example 1:'' Passing a string value between two running programs no matter where they are located.
;Example:Set 2 lines of text in the clipboard using a carriage return to end text lines
: ''Program1:''
{{CodeStart}}
{{CodeStart}}
{{Cl|PRINT}} "Start Program2 to read your text entries! Empty entry quits!"
{{Cl|DIM}} CrLf AS {{Cl|STRING}} * 2            'define as 2 byte STRING
{{Cl|_CLIPBOARD$ (statement)|_CLIPBOARD$}} = "Entry program started!"            'set clipboard initially
CrLf = {{Cl|CHR$}}(13) + {{Cl|CHR$}}(10)        'carriage return & line feed


DO
{{Cl|_CLIPBOARD$}} = "This is line 1" + CrLf + "This is line 2"
  {{Cl|LINE INPUT}} "Enter some text to send to other program: ", text$
{{Cl|PRINT}} {{Cl|_CLIPBOARD$ (function)|CLIPBOARD$}}                 'display what is in the clipboard
  {{Cl|IF...THEN|IF}} text$ = "" {{Cl|THEN}} {{Cl|EXIT DO}}
  {{Cl|_CLIPBOARD$ (statement)|_CLIPBOARD$}} = text$
{{Cl|LOOP}}
 
{{Cl|SYSTEM}}
{{CodeEnd}}
:''Program2:''
{{CodeStart}}
{{Cl|PRINT}} "Enter text in Program1 and this program will read it. Esc key quits!"
 
DO: {{Cl|_LIMIT}} 100
  text$ = {{Cl|_CLIPBOARD$}}                      'function returns clipboard contents
  {{Cl|IF...THEN|IF}} {{Cl|LEN}}(text$) {{Cl|THEN}}
    {{Cl|PRINT}} text$
    {{Cl|_CLIPBOARD$ (statement)|_CLIPBOARD$}} = ""                      'clear clipboard after a read
  {{Cl|END IF}}
{{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INKEY$}} = {{Cl|CHR$}}(27)
 
{{Cl|END}}
{{CodeEnd}}
:''Explanation:'' Compile and run both programs at once to see the interaction. You could also run them on different paths.
 
 
''Example 2: A minimized program that pops up when Ctrl + Shift is entered anytime in '''Windows''' and adds clipboard text to be Pasted:
{{CodeStart}}
'"ClippyBoard" program uses GetKeyState Win API to monitor a specific key combination.
'This demo will maximize the window and focus on program when Shift+A is pressed.
 
{{Cl|DECLARE DYNAMIC LIBRARY}} "user32"
  {{Cl|FUNCTION}} FindWindowA%& ({{Cl|BYVAL}} ClassName {{Cl|AS}} {{Cl|_OFFSET}}, WindowName$) 'find process handle by title
  {{Cl|FUNCTION}} GetKeyState% ({{Cl|BYVAL}} nVirtKey {{Cl|AS}} {{Cl|LONG}}) 'Windows virtual key presses
  {{Cl|FUNCTION}} ShowWindow& ({{Cl|BYVAL}} hwnd {{Cl|AS}} {{Cl|_OFFSET}}, {{Cl|BYVAL}} nCmdShow {{Cl|AS}} {{Cl|LONG}}) 'maximize process
  {{Cl|FUNCTION}} GetForegroundWindow%& 'find currently focused process handle
  {{Cl|FUNCTION}} SetForegroundWindow& ({{Cl|BYVAL}} hwnd {{Cl|AS}} {{Cl|_OFFSET}}) 'set foreground window process(focus)
{{Cl|DECLARE LIBRARY|END DECLARE}}
 
title$ = "Clippy Clipboard (Ctrl+Shift)" 'title of program window
{{Cl|_TITLE}} title$ 'set program title
hwnd%& = FindWindowA(0, title$ + {{Cl|CHR$}}(0)) 'find this program's process handle
 
{{Cl|SCREEN}} 13
{{Cl|_SCREENMOVE}} {{Cl|_SCREENMOVE|_MIDDLE}}
 
{{Cl|COLOR}} 10: {{Cl|PRINT}}
{{Cl|PRINT}} " Press Ctrl+Shift to see clipboard menu."
 
{{Cl|_DELAY}} 4
x& = ShowWindow&(hwnd%&, 2) 'minimize
 
{{Cl|DO...LOOP|DO}}: {{Cl|_LIMIT}} 30 'save CPU usage while waiting for key press
 
  {{Cl|IF...THEN|IF}} GetKeyState(16) < 0 {{Cl|AND (boolean)|AND}} GetKeyState(17) < 0 {{Cl|THEN}} '<==== Shift+A
    FGwin%& = GetForegroundWindow%& 'get current process in focus
    y& = ShowWindow&(hwnd%&, 1) 'maximize minimized program
 
    {{Cl|IF...THEN|IF}} FGwin%& <> hwnd%& {{Cl|THEN}} z& = SetForegroundWindow&(hwnd%&) 'set focus when necessary
    {{Cl|_DELAY}} 1
    GetKey
    x& = ShowWindow&(hwnd%&, 2) 'minimize after letter key entry
    {{Cl|COLOR}} 10: {{Cl|PRINT}}
    {{Cl|PRINT}} " Press Ctrl+Shift to see clipboard menu."
  {{Cl|END IF}}
{{Cl|LOOP}}
 
 
{{Cl|SUB}} GetKey
{{Cl|CLS}}
{{Cl|COLOR}} 12: {{Cl|PRINT}}: {{Cl|PRINT}} {{Cl|_CLIPBOARD$}}
{{Cl|DO...LOOP|DO}}: {{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INKEY$}} = ""
{{Cl|_DELAY}} 1
{{Cl|CLS}}
{{Cl|COLOR}} 11: {{Cl|PRINT}} "Select a letter clipboard option:"
{{Cl|PRINT}}
{{Cl|PRINT}} "A = Address"
{{Cl|PRINT}} "C = Cell phone"
{{Cl|PRINT}} "E = Email"
{{Cl|PRINT}} "F = First Name"
{{Cl|PRINT}} "H = Home phone"
{{Cl|PRINT}} "L = Last Name"
{{Cl|PRINT}} "N = Name"
{{Cl|PRINT}} "M = MAC address"
{{Cl|PRINT}} "P = Password"
{{Cl|PRINT}} "W = Work name"
{{Cl|PRINT}} "X = QUIT!"
{{Cl|PRINT}} "Z = Zip code"
{{Cl|COLOR}} 14: {{Cl|PRINT}}
{{Cl|PRINT}} "Another letter will skip or X = {{Cl|EXIT}}!"
 
K$ = {{Cl|UCASE$}}({{Cl|INPUT$}}(1))
 
{{Cl|SELECT CASE}} K$  'The following text should be your personal user info:
  {{Cl|CASE}} "A": {{Cl|_CLIPBOARD$ (statement)|_CLIPBOARD$}} = "my address"
  {{Cl|CASE}} "C": {{Cl|_CLIPBOARD$ (statement)|_CLIPBOARD$}} = "cell number"
  {{Cl|CASE}} "E": {{Cl|_CLIPBOARD$ (statement)|_CLIPBOARD$}} = "myemail"
  {{Cl|CASE}} "F": {{Cl|_CLIPBOARD$ (statement)|_CLIPBOARD$}} = "formal name"
  {{Cl|CASE}} "H": {{Cl|_CLIPBOARD$ (statement)|_CLIPBOARD$}} = "home number"
  {{Cl|CASE}} "L": {{Cl|_CLIPBOARD$ (statement)|_CLIPBOARD$}} = "lastname"
  {{Cl|CASE}} "M": {{Cl|_CLIPBOARD$ (statement)|_CLIPBOARD$}} = "modempassword"
  {{Cl|CASE}} "N": {{Cl|_CLIPBOARD$ (statement)|_CLIPBOARD$}} = "name"
  {{Cl|CASE}} "P": {{Cl|_CLIPBOARD$ (statement)|_CLIPBOARD$}} = "password"
  {{Cl|CASE}} "X": {{Cl|END}}
  {{Cl|CASE}} "Z": {{Cl|_CLIPBOARD$ (statement)|_CLIPBOARD$}} = "zipcode"
{{Cl|END SELECT}}
{{Cl|CLS}}
{{Cl|PRINT}}
{{Cl|PRINT}}
{{Cl|COLOR}} 14: {{Cl|PRINT}} {{Cl|_CLIPBOARD$}}
{{Cl|BEEP}}
{{Cl|_DELAY}} 2
{{Cl|END SUB}}
{{CodeEnd}}
{{CodeEnd}}
{{Small|Code by Ted Weissgerber}}
{{OutputStart}}
: ''Explanation:'' The program will run minimized until Ctrl + Shift is entered and will pop up to ask for a letter choice that contains the text you want in the clipboard. More letter choices can be added. Text can be pasted into a web page or entry box and the program will minimize until it is needed later. The program uses very little resources!
This is line 1
This is line 2
{{OutputEnd}}




{{PageSeeAlso}}
{{PageSeeAlso}}
* [[_CLIPBOARD$ (statement)]]
* [[_CLIPBOARD$ (function)]]
* [[_CLIPBOARDIMAGE (function)]], [[_CLIPBOARDIMAGE]] (statement)
* [[_CLIPBOARDIMAGE (function)]], [[_CLIPBOARDIMAGE]] (statement)
* [[CHR$]], [[ASCII]] (code table)




{{PageNavigation}}
{{PageNavigation}}

Revision as of 23:49, 24 February 2023

The _CLIPBOARD$ statement copies the STRING value into the system clipboard.


Syntax

_CLIPBOARD$ = string_expression$


Description

  • string_expression$ is the string value to be sent to the clipboard.
  • The string value will replace everything previously in the clipboard.
  • Assemble long text into one string variable value before using this statement.
  • Add CHR$(13) + CHR$(10) CRLF characters to move to a new clipboard line.
  • When copying text files, end line CRLF characters 13 and 10 do not have to be added.
  • Numerical values can be converted to strings using STR$, _MK$, MKI$, MKL$, MKS$, MKD$, _BIN$, HEX$ or OCT$.
  • The clipboard can be used to copy, paste and communicate between running programs.


Examples

Example
Set 2 lines of text in the clipboard using a carriage return to end text lines
DIM CrLf AS STRING * 2            'define as 2 byte STRING
CrLf = CHR$(13) + CHR$(10)        'carriage return & line feed

_CLIPBOARD$ = "This is line 1" + CrLf + "This is line 2"
PRINT CLIPBOARD$                 'display what is in the clipboard
This is line 1
This is line 2


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link