CLIPBOARD$ (function): 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$ (function)}}
{{DISPLAYTITLE:_CLIPBOARD$ (function)}}
The [[_CLIPBOARD$]] function returns the current Operating System's clipboard contents as a [[STRING]].
The '''_CLIPBOARD$''' function returns the current Operating System's clipboard contents as a [[STRING]].




{{PageSyntax}}
{{PageSyntax}}
:{{Parameter|result$}} = [[_CLIPBOARD$]]
:{{Parameter|result$}} = [[_CLIPBOARD$ (function)|_CLIPBOARD$]]




{{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.
* Text returned can contain the entire contents of a copied file or web page or text from a previous [[_CLIPBOARD$]] statement.
* The string returned can also contain formatting like CRLF ([[CHR$]](13) + [[CHR$]](10)) end of line characters.
* The string returned can also contain formatting like CRLF ([[CHR$]](13) + [[CHR$]](10)) end of line characters.
* 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.
Line 14: Line 14:


{{PageExamples}}
{{PageExamples}}
''Example 1:'' Passing a string value between two running programs no matter where they are located.
;Example 1:Passing a string value between two running programs no matter where they are located.
: ''Program1:''
{{CodeStart}}
{{CodeStart}}
'=================
'=== Program 1 ===
'=================
{{Cl|PRINT}} "Start Program2 to read your text entries! Empty entry quits!"
{{Cl|PRINT}} "Start Program2 to read your text entries! Empty entry quits!"
{{Cl|_CLIPBOARD$ (statement)|_CLIPBOARD$}} = "Entry program started!"            'set clipboard initially
{{Cl|_CLIPBOARD$}} = "Entry program started!"            'set clipboard initially


DO
DO
   {{Cl|LINE INPUT}} "Enter some text to send to other program: ", text$
   {{Cl|LINE INPUT}} "Enter some text to send to other program: ", text$
   {{Cl|IF...THEN|IF}} text$ = "" {{Cl|THEN}} {{Cl|EXIT DO}}
   {{Cl|IF...THEN|IF}} text$ = "" {{Cl|THEN}} {{Cl|EXIT DO}}
   {{Cl|_CLIPBOARD$ (statement)|_CLIPBOARD$}} = text$
   {{Cl|_CLIPBOARD$}} = text$
{{Cl|LOOP}}
{{Cl|LOOP}}


{{Cl|SYSTEM}}
{{Cl|SYSTEM}}
{{CodeEnd}}
{{CodeEnd}}
:''Program2:''
{{CodeStart}}
{{CodeStart}}
'=================
'=== Program 2 ===
'=================
{{Cl|PRINT}} "Enter text in Program1 and this program will read it. Esc key quits!"
{{Cl|PRINT}} "Enter text in Program1 and this program will read it. Esc key quits!"


DO: {{Cl|_LIMIT}} 100
DO: {{Cl|_LIMIT}} 100
   text$ = {{Cl|_CLIPBOARD$}}                      'function returns clipboard contents
   text$ = {{Cl|_CLIPBOARD$ (function)|CLIPBOARD$}}                      'function returns clipboard contents
   {{Cl|IF...THEN|IF}} {{Cl|LEN}}(text$) {{Cl|THEN}}
   {{Cl|IF...THEN|IF}} {{Cl|LEN}}(text$) {{Cl|THEN}}
     {{Cl|PRINT}} text$
     {{Cl|PRINT}} text$
     {{Cl|_CLIPBOARD$ (statement)|_CLIPBOARD$}} = ""                      'clear clipboard after a read
     {{Cl|_CLIPBOARD$}} = ""                      'clear clipboard after a read
   {{Cl|END IF}}
   {{Cl|END IF}}
{{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INKEY$}} = {{Cl|CHR$}}(27)
{{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INKEY$}} = {{Cl|CHR$}}(27)
Line 44: Line 50:
:''Explanation:'' Compile and run both programs at once to see the interaction. You could also run them on different paths.
:''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:
;Example 2:A minimized program that pops up when Ctrl + Shift is entered anytime in '''Windows''' and adds clipboard text to be Pasted:
{{CodeStart}}
{{CodeStart}}
'"ClippyBoard" program uses GetKeyState Win API to monitor a specific key combination.
'"ClippyBoard" program uses GetKeyState Win API to monitor a specific key combination.
Line 89: Line 96:
{{Cl|SUB}} GetKey
{{Cl|SUB}} GetKey
{{Cl|CLS}}
{{Cl|CLS}}
{{Cl|COLOR}} 12: {{Cl|PRINT}}: {{Cl|PRINT}} {{Cl|_CLIPBOARD$}}
{{Cl|COLOR}} 12: {{Cl|PRINT}}: {{Cl|PRINT}} {{Cl|_CLIPBOARD$ (function)|CLIPBOARD$}}
{{Cl|DO...LOOP|DO}}: {{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INKEY$}} = ""
{{Cl|DO...LOOP|DO}}: {{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INKEY$}} = ""
{{Cl|_DELAY}} 1
{{Cl|_DELAY}} 1
Line 113: Line 120:


{{Cl|SELECT CASE}} K$  'The following text should be your personal user info:
{{Cl|SELECT CASE}} K$  'The following text should be your personal user info:
   {{Cl|CASE}} "A": {{Cl|_CLIPBOARD$ (statement)|_CLIPBOARD$}} = "my address"
   {{Cl|CASE}} "A": {{Cl|_CLIPBOARD$}} = "my address"
   {{Cl|CASE}} "C": {{Cl|_CLIPBOARD$ (statement)|_CLIPBOARD$}} = "cell number"
   {{Cl|CASE}} "C": {{Cl|_CLIPBOARD$}} = "cell number"
   {{Cl|CASE}} "E": {{Cl|_CLIPBOARD$ (statement)|_CLIPBOARD$}} = "myemail"
   {{Cl|CASE}} "E": {{Cl|_CLIPBOARD$}} = "myemail"
   {{Cl|CASE}} "F": {{Cl|_CLIPBOARD$ (statement)|_CLIPBOARD$}} = "formal name"
   {{Cl|CASE}} "F": {{Cl|_CLIPBOARD$}} = "formal name"
   {{Cl|CASE}} "H": {{Cl|_CLIPBOARD$ (statement)|_CLIPBOARD$}} = "home number"
   {{Cl|CASE}} "H": {{Cl|_CLIPBOARD$}} = "home number"
   {{Cl|CASE}} "L": {{Cl|_CLIPBOARD$ (statement)|_CLIPBOARD$}} = "lastname"
   {{Cl|CASE}} "L": {{Cl|_CLIPBOARD$}} = "lastname"
   {{Cl|CASE}} "M": {{Cl|_CLIPBOARD$ (statement)|_CLIPBOARD$}} = "modempassword"
   {{Cl|CASE}} "M": {{Cl|_CLIPBOARD$}} = "modempassword"
   {{Cl|CASE}} "N": {{Cl|_CLIPBOARD$ (statement)|_CLIPBOARD$}} = "name"
   {{Cl|CASE}} "N": {{Cl|_CLIPBOARD$}} = "name"
   {{Cl|CASE}} "P": {{Cl|_CLIPBOARD$ (statement)|_CLIPBOARD$}} = "password"
   {{Cl|CASE}} "P": {{Cl|_CLIPBOARD$}} = "password"
   {{Cl|CASE}} "X": {{Cl|END}}
   {{Cl|CASE}} "X": {{Cl|END}}
   {{Cl|CASE}} "Z": {{Cl|_CLIPBOARD$ (statement)|_CLIPBOARD$}} = "zipcode"
   {{Cl|CASE}} "Z": {{Cl|_CLIPBOARD$}} = "zipcode"
{{Cl|END SELECT}}
{{Cl|END SELECT}}
{{Cl|CLS}}
{{Cl|CLS}}
{{Cl|PRINT}}
{{Cl|PRINT}}
{{Cl|PRINT}}
{{Cl|PRINT}}
{{Cl|COLOR}} 14: {{Cl|PRINT}} {{Cl|_CLIPBOARD$}}
{{Cl|COLOR}} 14: {{Cl|PRINT}} {{Cl|_CLIPBOARD$ (function)|CLIPBOARD$}}
{{Cl|BEEP}}
{{Cl|BEEP}}
{{Cl|_DELAY}} 2
{{Cl|_DELAY}} 2
Line 138: Line 145:


{{PageSeeAlso}}
{{PageSeeAlso}}
* [[_CLIPBOARD$ (statement)]]
* [[_CLIPBOARD$]] (statement)
* [[_CLIPBOARDIMAGE (function)]], [[_CLIPBOARDIMAGE]] (statement)
* [[_CLIPBOARDIMAGE (function)]], [[_CLIPBOARDIMAGE]] (statement)




{{PageNavigation}}
{{PageNavigation}}

Revision as of 23:49, 24 February 2023

The _CLIPBOARD$ function returns the current Operating System's clipboard contents as a STRING.


Syntax

result$ = _CLIPBOARD$


Description

  • Text returned can contain the entire contents of a copied file or web page or text from a previous _CLIPBOARD$ statement.
  • The string returned can also contain formatting like CRLF (CHR$(13) + CHR$(10)) end of line characters.
  • The clipboard can be used to copy, paste and communicate between running programs.


Examples

Example 1
Passing a string value between two running programs no matter where they are located.
'=================
'=== Program 1 ===
'=================

PRINT "Start Program2 to read your text entries! Empty entry quits!"
_CLIPBOARD$ = "Entry program started!"            'set clipboard initially

DO
  LINE INPUT "Enter some text to send to other program: ", text$
  IF text$ = "" THEN EXIT DO
  _CLIPBOARD$ = text$
LOOP

SYSTEM
'=================
'=== Program 2 ===
'=================

PRINT "Enter text in Program1 and this program will read it. Esc key quits!"

DO: _LIMIT 100
  text$ = CLIPBOARD$                       'function returns clipboard contents
  IF LEN(text$) THEN
     PRINT text$
     _CLIPBOARD$ = ""                       'clear clipboard after a read
  END IF
LOOP UNTIL INKEY$ = CHR$(27)

END
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:
'"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.

DECLARE DYNAMIC LIBRARY "user32"
  FUNCTION FindWindowA%& (BYVAL ClassName AS _OFFSET, WindowName$) 'find process handle by title
  FUNCTION GetKeyState% (BYVAL nVirtKey AS LONG) 'Windows virtual key presses
  FUNCTION ShowWindow& (BYVAL hwnd AS _OFFSET, BYVAL nCmdShow AS LONG) 'maximize process
  FUNCTION GetForegroundWindow%& 'find currently focused process handle
  FUNCTION SetForegroundWindow& (BYVAL hwnd AS _OFFSET) 'set foreground window process(focus)
END DECLARE

title$ = "Clippy Clipboard (Ctrl+Shift)" 'title of program window
_TITLE title$ 'set program title
hwnd%& = FindWindowA(0, title$ + CHR$(0)) 'find this program's process handle

SCREEN 13
_SCREENMOVE _MIDDLE

COLOR 10: PRINT
PRINT " Press Ctrl+Shift to see clipboard menu."

_DELAY 4
x& = ShowWindow&(hwnd%&, 2) 'minimize

DO: _LIMIT 30 'save CPU usage while waiting for key press

  IF GetKeyState(16) < 0 AND GetKeyState(17) < 0 THEN '<==== Shift+A
    FGwin%& = GetForegroundWindow%& 'get current process in focus
    y& = ShowWindow&(hwnd%&, 1) 'maximize minimized program

    IF FGwin%& <> hwnd%& THEN z& = SetForegroundWindow&(hwnd%&) 'set focus when necessary
    _DELAY 1
    GetKey
    x& = ShowWindow&(hwnd%&, 2) 'minimize after letter key entry
    COLOR 10: PRINT
    PRINT " Press Ctrl+Shift to see clipboard menu."
  END IF
LOOP


SUB GetKey
CLS
COLOR 12: PRINT: PRINT CLIPBOARD$
DO: LOOP UNTIL INKEY$ = ""
_DELAY 1
CLS
COLOR 11: PRINT "Select a letter clipboard option:"
PRINT
PRINT "A = Address"
PRINT "C = Cell phone"
PRINT "E = Email"
PRINT "F = First Name"
PRINT "H = Home phone"
PRINT "L = Last Name"
PRINT "N = Name"
PRINT "M = MAC address"
PRINT "P = Password"
PRINT "W = Work name"
PRINT "X = QUIT!"
PRINT "Z = Zip code"
COLOR 14: PRINT
PRINT "Another letter will skip or X = EXIT!"

K$ = UCASE$(INPUT$(1))

SELECT CASE K$  'The following text should be your personal user info:
  CASE "A": _CLIPBOARD$ = "my address"
  CASE "C": _CLIPBOARD$ = "cell number"
  CASE "E": _CLIPBOARD$ = "myemail"
  CASE "F": _CLIPBOARD$ = "formal name"
  CASE "H": _CLIPBOARD$ = "home number"
  CASE "L": _CLIPBOARD$ = "lastname"
  CASE "M": _CLIPBOARD$ = "modempassword"
  CASE "N": _CLIPBOARD$ = "name"
  CASE "P": _CLIPBOARD$ = "password"
  CASE "X": END
  CASE "Z": _CLIPBOARD$ = "zipcode"
END SELECT
CLS
PRINT
PRINT
COLOR 14: PRINT CLIPBOARD$
BEEP
_DELAY 2
END SUB
Code by Ted Weissgerber
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!


See also



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