操作系统独立剪贴板复制/粘贴C中的文本

时间:2022-11-09 10:44:21

I'm working on a project that's supposed to work on both Windows and Linux (with an unofficial Mac port as well) that emulates a true colour system console.

我正在开发一个应该在Windows和Linux上工作的项目(还有一个非官方的Mac端口),可以模拟真正的色彩系统控制台。

My problem is that recently there appeared a request for textfield support (yes, console-based) and it would be cool to add the possibility of copying text to clipboard and pasting from it. Is there a way of achieving this that will:

我的问题是,最近出现了对文本字段支持的请求(是的,基于控制台),添加将文本复制到剪贴板并从中粘贴的可能性会很酷。有没有办法实现这一目标:

  • be done in C (not C++),
  • 用C语言(不是C ++)完成,

  • work in both Windows and in Linux (preprocessor macros are an option if there's no platform-independent code),
  • 在Windows和Linux中工作(如果没有与平台无关的代码,预处理器宏是一个选项),

  • require no extra libraries to link to?
  • 不需要额外的库链接到?

Thanks in advance for your help.

在此先感谢您的帮助。

4 个解决方案

#1


5  

If you're not using a cross platform UI library (like wx or something), then it sounds like you're just going to have to write native clipboard code for each platform you want to support.

如果您没有使用跨平台UI库(如wx或其他东西),那么听起来您只需要为要支持的每个平台编写本机剪贴板代码。

Remember, on Macintoshes, you copy with Command-C, not Ctrl+C :)

请记住,在Macintoshes上,您使用Command-C复制,而不是Ctrl + C :)

#2


4  

The clipboard is inherently an operating system defined concept. The C language itself has no knowledge of what a clipboard is or how to operate on it. You must either interface directly with the OS, or use a portability library that does this on your behalf. There is no way around this.

剪贴板本质上是操作系统定义的概念。 C语言本身不知道剪贴板是什么或如何操作它。您必须直接与操作系统接口,或使用代表您执行此操作的可移植性库。没有办法解决这个问题。

#3


4  

Personally I would define my your own function

就个人而言,我会定义我自己的功能

getClipboardText();

That is defined in two different header files (linux_clipboard.h, windows_clipboard.h, etc) and then do pre-proccessor stuff to load the appropriate one accordingly. I don't really code in C/C++ so I'm sorry if that didn't make any sense or is bad practice but that's how I'd go about doing this.

这是在两个不同的头文件(linux_clipboard.h,windows_clipboard.h等)中定义的,然后执行预处理程序以相应地加载适当的头文件。我真的没有用C / C ++编写代码,所以如果这没有任何意义或者是不好的做法我很抱歉,但这就是我要做的事情。

#if WIN32
#include windows_clipboard.h
#endif

That sort of thing

之类的东西

Remember: For linux you have to deal with different window managers (Gnome, KDE) all with different ways of managing the clipboard. Keep this in mind when designing your app.

请记住:对于Linux,您必须使用不同的管理剪贴板的方式处理不同的窗口管理器(Gnome,KDE)。在设计应用时请记住这一点。

#4


1  

You may be able to communicate to the clipboard by using xclip. You can use this python script here to do this job via communicating with 'dcop' and 'klipper' here. That is for KDE, I do not know how it would be done under GNOME... You may also be able to do this independantly of either GNOME/KDE by using DBUS, although I cannot say 100% confidently on that either...

您可以使用xclip与剪贴板进行通信。你可以在这里使用这个python脚本通过与'dcop'和'klipper'进行通信来完成这项工作。这是针对KDE的,我不知道它是如何在GNOME下完成的......你也可以通过使用DBUS独立完成GNOME / KDE,尽管我不能100%自信地说...

Just be aware, that for a truly cross-platform job, you have to take into account of the different GUI's such as under Linux, X is the main window manager interface and either GNOME/KDE sits on top of it..I am not singling out other GUI's such as FluxBox, WindowMaker to name but a few, and that there will be a lot of platform dependant code, and also in conjunction, you will be dealing with Windows clipboard as well..all in all, a big integrated code...

请注意,对于真正的跨平台工作,您必须考虑不同的GUI,例如在Linux下,X是主窗口管理器界面,GNOME / KDE位于它之上......我不是单挑其他GUI,如FluxBox,WindowMaker,仅举几例,并且会有很多平台相关的代码,同时也会同时处理Windows剪贴板......总而言之,这是一个很大的集成码...

Have you not considered looking at the raw X programming API for clipboard support? Maybe that might be better as I would imagine, GNOME/KDE etc are using the X's API to do the clipboard work...if that is confirmed, then the work would be cut out and be independant of the major GUI interfaces...(I hope that would be the case as it would make life easier for your project!)

您是否考虑过查看原始X编程API以获得剪贴板支持?也许这可能会更好,正如我想象的那样,GNOME / KDE等正在使用X的API进行剪贴板工作......如果确认了,那么工作将被删除,并且独立于主要的GUI界面...... (我希望情况会如此,因为它会让你的项目更轻松!)

Perhaps using compile-time switches, for each platform...WIN, KDE, GNOME, MAC or use the one that is already pre-defined..

也许使用编译时开关,为每个平台... WIN,KDE,GNOME,MAC或使用已经预定义的那个。

Hope this helps, Best regards, Tom.

希望这会有所帮助,最好的问候,汤姆。

#1


5  

If you're not using a cross platform UI library (like wx or something), then it sounds like you're just going to have to write native clipboard code for each platform you want to support.

如果您没有使用跨平台UI库(如wx或其他东西),那么听起来您只需要为要支持的每个平台编写本机剪贴板代码。

Remember, on Macintoshes, you copy with Command-C, not Ctrl+C :)

请记住,在Macintoshes上,您使用Command-C复制,而不是Ctrl + C :)

#2


4  

The clipboard is inherently an operating system defined concept. The C language itself has no knowledge of what a clipboard is or how to operate on it. You must either interface directly with the OS, or use a portability library that does this on your behalf. There is no way around this.

剪贴板本质上是操作系统定义的概念。 C语言本身不知道剪贴板是什么或如何操作它。您必须直接与操作系统接口,或使用代表您执行此操作的可移植性库。没有办法解决这个问题。

#3


4  

Personally I would define my your own function

就个人而言,我会定义我自己的功能

getClipboardText();

That is defined in two different header files (linux_clipboard.h, windows_clipboard.h, etc) and then do pre-proccessor stuff to load the appropriate one accordingly. I don't really code in C/C++ so I'm sorry if that didn't make any sense or is bad practice but that's how I'd go about doing this.

这是在两个不同的头文件(linux_clipboard.h,windows_clipboard.h等)中定义的,然后执行预处理程序以相应地加载适当的头文件。我真的没有用C / C ++编写代码,所以如果这没有任何意义或者是不好的做法我很抱歉,但这就是我要做的事情。

#if WIN32
#include windows_clipboard.h
#endif

That sort of thing

之类的东西

Remember: For linux you have to deal with different window managers (Gnome, KDE) all with different ways of managing the clipboard. Keep this in mind when designing your app.

请记住:对于Linux,您必须使用不同的管理剪贴板的方式处理不同的窗口管理器(Gnome,KDE)。在设计应用时请记住这一点。

#4


1  

You may be able to communicate to the clipboard by using xclip. You can use this python script here to do this job via communicating with 'dcop' and 'klipper' here. That is for KDE, I do not know how it would be done under GNOME... You may also be able to do this independantly of either GNOME/KDE by using DBUS, although I cannot say 100% confidently on that either...

您可以使用xclip与剪贴板进行通信。你可以在这里使用这个python脚本通过与'dcop'和'klipper'进行通信来完成这项工作。这是针对KDE的,我不知道它是如何在GNOME下完成的......你也可以通过使用DBUS独立完成GNOME / KDE,尽管我不能100%自信地说...

Just be aware, that for a truly cross-platform job, you have to take into account of the different GUI's such as under Linux, X is the main window manager interface and either GNOME/KDE sits on top of it..I am not singling out other GUI's such as FluxBox, WindowMaker to name but a few, and that there will be a lot of platform dependant code, and also in conjunction, you will be dealing with Windows clipboard as well..all in all, a big integrated code...

请注意,对于真正的跨平台工作,您必须考虑不同的GUI,例如在Linux下,X是主窗口管理器界面,GNOME / KDE位于它之上......我不是单挑其他GUI,如FluxBox,WindowMaker,仅举几例,并且会有很多平台相关的代码,同时也会同时处理Windows剪贴板......总而言之,这是一个很大的集成码...

Have you not considered looking at the raw X programming API for clipboard support? Maybe that might be better as I would imagine, GNOME/KDE etc are using the X's API to do the clipboard work...if that is confirmed, then the work would be cut out and be independant of the major GUI interfaces...(I hope that would be the case as it would make life easier for your project!)

您是否考虑过查看原始X编程API以获得剪贴板支持?也许这可能会更好,正如我想象的那样,GNOME / KDE等正在使用X的API进行剪贴板工作......如果确认了,那么工作将被删除,并且独立于主要的GUI界面...... (我希望情况会如此,因为它会让你的项目更轻松!)

Perhaps using compile-time switches, for each platform...WIN, KDE, GNOME, MAC or use the one that is already pre-defined..

也许使用编译时开关,为每个平台... WIN,KDE,GNOME,MAC或使用已经预定义的那个。

Hope this helps, Best regards, Tom.

希望这会有所帮助,最好的问候,汤姆。