我试图使用以下代码临时将System.out重定向到/ dev/null但它不起作用.
System.out.println("this should go to stdout");
PrintStream original = System.out;
System.setOut(new PrintStream(new FileOutputStream("/dev/null")));
System.out.println("this should go to /dev/null");
System.setOut(original);
System.out.println("this should go to stdout"); // This is not getting printed!!!
Run Code Online (Sandbox Code Playgroud)
有人有主意吗?
我正在尝试为远程桌面型客户端获取所有键盘事件.我不希望像ALT-Tab这样的东西被Gnome3/KDE/Openbox/etc ...桌面抓住,我希望我的应用程序能够让所有这些事件和其他应用程序无法获得该事件.
我现在正在做这样的事情:
grabKeyboard() // qt function
Display *display = XOpenDisplay(NULL);
XGrabKeyboard(display, winId(), True, GrabModeAsync, GrabModeAsync, CurrentTime);
Run Code Online (Sandbox Code Playgroud)
这实际上似乎与ALT-Tab一起工作正常,但在Openbox中有一堆键盘快捷键被定义为"显示桌面"(ALT-CTRL-END)和"重置X"(CTRL-ALT-R)被捕获通过Openbox.我注意到FreeRDP做了这样的事情:
int input_mask =
KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
VisibilityChangeMask | FocusChangeMask | StructureNotifyMask |
PointerMotionMask | ExposureMask | PropertyChangeMask;
XSelectInput(display, winId(), input_mask);
Run Code Online (Sandbox Code Playgroud)
除了我上面的代码之外,我已经尝试了它,但它不起作用.
我也注意到Remmina使用gdk_device_grab,但由于我的应用程序不是GTK应用程序,我无法调用它.有人可以帮忙吗?
是否可以将基于类的视图委托给特定的基于类的视图?具体来说,我想做的是/指向一个名为'home'的视图,如果用户已登录,则指向View A的主视图;如果没有用户登录,则指向View B.或者我可以执行重定向到其他网址.我不确定这里最好的做法是什么.
可能重复:
Python While循环中的赋值条件
什么相当于:
while (line = p.stdout.readline()) != '':
...
Run Code Online (Sandbox Code Playgroud)
在Python?
我不喜欢这样做:
line = p.stdout.readline()
while line != '':
sys.stdout.write(line)
line = p.stdout.readline()
Run Code Online (Sandbox Code Playgroud)
虽然我多年来一直在使用后者......但我怀疑别无选择.我认为p.stdout
支持迭代,就像
for line in p.stdout:
sys.stdout.write(line)
Run Code Online (Sandbox Code Playgroud)
但不幸的是,它没有,open()
例如从返回的句柄.
编辑:对不起,我错了,它确实支持它,问题是我无法立刻把它拿出来,就像我可以p.stdout.readline()
.添加sys.stdout.flush()
似乎没有帮助,所以它必须在内部缓冲p.stdout
.
我们有一个类通过套接字与另一个主机通信,它看起来像这样:
SocketChannel sc = SocketChannel.open(new InetSocketAddress(HOST, PORT));
sc.configureBlocking(true);
...
sc.write(...)
sc.read(...)
Run Code Online (Sandbox Code Playgroud)
这个类工作得很好,除非主机关闭,然后 SocketChannel.open 永远阻塞。我尝试通过执行以下操作来超时:
SocketChannel = SocketChannel.open();
sc.configureBlocking(false);
boolean result = socketChannel.connect(new InetSocketAddress(HOST, PORT));
if (!result) {
long startTime = System.currentTimeMillis();
while (!socketChannel.finishConnect()) {
if (System.currentTimeMillis() - startTime< 1000) {
// keep trying
Thread.sleep(100);
} else {
// FAILED!
enabled = false;
return;
}
}
}
// SUCCESS!
socketChannel.configureBlocking(true);
enabled = true
Run Code Online (Sandbox Code Playgroud)
好吧,由于某种原因 finishConnect() 永远阻塞,而我本希望它根本不会阻塞。有任何想法吗?
List<PageInfo> subPages = new List<PageInfo>();
// ...
// some code to populate subPages here...
// ...
List<Guid> subPageGuids = new List<Guid> {from x in subPages select x.Id}; //doesn't work
Run Code Online (Sandbox Code Playgroud)
PageInfo有一个ID字段,类型为Guid.所以x.Id是一个System.Guid.
上面第二行代码不起作用......我收到错误:
集合初始化程序的最佳重载Add方法'System.Collections.Generic.List.Add(System.Guid)'有一些无效的参数
和
参数'1':无法从'System.Collections.Generic.IEnumerable'转换为'System.Guid'
我只用C#编写了大约一个星期,但我之前做过类似的模式,从来没有遇到过这个问题.